Sun, 01 Aug 2010 12:17:12 +0100
Add 'delete' command
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) |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | if expires ~= 0 then |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | return false, "Expiry is not currently implemented"; |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | _flags[key] = flags; |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | _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
|
21 | 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
|
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 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | function cache:get(key) |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | 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
|
26 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | |
13 | 28 | function cache:delete(key, time) |
29 | local existed = _data[key]; | |
30 | _flags[key], _data[key], _expires[key] = nil, nil, nil; | |
31 | if existed and time then | |
32 | if time <= max_expires_seconds then | |
33 | time = now() + time; | |
34 | end | |
35 | _deleted[key] = time; | |
36 | end | |
37 | return true, existed; | |
38 | end | |
39 | ||
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 | 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
|
41 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | return _M; |