util/memcache.lua

Sun, 01 Aug 2010 12:17:12 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 01 Aug 2010 12:17:12 +0100
changeset 13
db66a09dc0b6
parent 0
73bc20975514
child 14
578214a34ded
permissions
-rw-r--r--

Add 'delete' command

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)
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
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
28 function cache:delete(key, time)
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
29 local existed = _data[key];
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
30 _flags[key], _data[key], _expires[key] = nil, nil, nil;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
31 if existed and time then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
32 if time <= max_expires_seconds then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
33 time = now() + time;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
34 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
35 _deleted[key] = time;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
36 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
37 return true, existed;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
38 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
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;

mercurial