Sun, 01 Aug 2010 12:40:45 +0100
Add 'add' and 'replace' commands
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 |
17
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
23 | _deleted[key] = nil; |
0
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | _data[key] = data; |
17
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
25 | return true, true; |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
26 | end |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
27 | |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
28 | function cache:add(key, flags, expires, data) |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
29 | if not(_data[key]) and (not(_deleted[key]) or _deleted[key] < now()) then |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
30 | return self:set(key, flags, expires, data); |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
31 | end |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
32 | return true, false; -- No error, but data was not stored |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
33 | end |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
34 | |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
35 | function cache:replace(key, flags, expires, data) |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
36 | if _data[key] and (not(_deleted[key]) or _deleted[key] < now()) then |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
37 | return self:set(key, flags, expires, data); |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
38 | end |
5bede08f2f55
Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
39 | return true, false; -- No error, but data was not stored |
0
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | |
16
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
42 | function cache:exists(key) |
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
43 | return not not _data[key]; |
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
44 | end |
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
45 | |
0
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | function cache:get(key) |
14
578214a34ded
Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
47 | 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
|
48 | if expires and expires < now() then |
14
578214a34ded
Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
49 | self:delete(key); |
578214a34ded
Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
50 | 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
|
51 | 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
|
52 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | |
13 | 54 | function cache:delete(key, time) |
55 | 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
|
56 | _flags[key], _data[key], _expiry[key] = nil, nil, nil; |
13 | 57 | if existed and time then |
58 | if time <= max_expires_seconds then | |
59 | time = now() + time; | |
60 | end | |
61 | _deleted[key] = time; | |
62 | end | |
63 | return true, existed; | |
64 | end | |
65 | ||
0
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | 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
|
67 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | return _M; |