util/memcache.lua

Sun, 01 Aug 2010 12:20:27 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 01 Aug 2010 12:20:27 +0100
changeset 15
87ef478bf7fc
parent 14
578214a34ded
child 16
906615e293de
permissions
-rw-r--r--

util.memcache: Fix backwards expiry logic and nil global access

13
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
1 local now = os.time;
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 module("memcache", package.seeall);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
13
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
5 local max_expires_seconds = 60*60*24*30;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
6
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 function new()
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local cache = {};
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local _data = {};
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local _flags = {};
13
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
12 local _expiry = {};
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
13 local _deleted = {};
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 function cache:set(key, flags, expires, data)
14
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
16 _flags[key] = flags;
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
17 if expires > 0 then
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
18 if expires <= max_expires_seconds then
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
19 expires = now() + expires;
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
20 end
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
21 _expiry[key] = expires;
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 _data[key] = data;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return true;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 function cache:get(key)
14
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
28 local expires = _expiry[key];
15
87ef478bf7fc util.memcache: Fix backwards expiry logic and nil global access
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
29 if expires and expires < now() then
14
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
30 self:delete(key);
578214a34ded Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
31 end
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 return _flags[key], _data[key];
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
13
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
35 function cache:delete(key, time)
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
36 local existed = _data[key];
15
87ef478bf7fc util.memcache: Fix backwards expiry logic and nil global access
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
37 _flags[key], _data[key], _expiry[key] = nil, nil, nil;
13
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
38 if existed and time then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
39 if time <= max_expires_seconds then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
40 time = now() + time;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
41 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
42 _deleted[key] = time;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
43 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
44 return true, existed;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
45 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
46
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 return cache;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return _M;

mercurial