Sun, 01 Aug 2010 12:20:27 +0100
util.memcache: Fix backwards expiry logic and nil global access
13 | 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 | 5 | local max_expires_seconds = 60*60*24*30; |
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 | 12 | local _expiry = {}; |
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 | 35 | function cache:delete(key, time) |
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 | 38 | if existed and time then |
39 | if time <= max_expires_seconds then | |
40 | time = now() + time; | |
41 | end | |
42 | _deleted[key] = time; | |
43 | end | |
44 | return true, existed; | |
45 | end | |
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; |