Sun, 01 Aug 2010 12:23:34 +0100
util.memcache: Add cache:exists(key)
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 | |
16
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
27 | function cache:exists(key) |
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
28 | return not not _data[key]; |
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
29 | end |
906615e293de
util.memcache: Add cache:exists(key)
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
30 | |
0
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | function cache:get(key) |
14
578214a34ded
Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
32 | 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
|
33 | if expires and expires < now() then |
14
578214a34ded
Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
34 | self:delete(key); |
578214a34ded
Implement initial expiry support
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
35 | 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
|
36 | 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
|
37 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | |
13 | 39 | function cache:delete(key, time) |
40 | 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
|
41 | _flags[key], _data[key], _expiry[key] = nil, nil, nil; |
13 | 42 | if existed and time then |
43 | if time <= max_expires_seconds then | |
44 | time = now() + time; | |
45 | end | |
46 | _deleted[key] = time; | |
47 | end | |
48 | return true, existed; | |
49 | end | |
50 | ||
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 cache; |
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 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | return _M; |