util/memcache.lua

changeset 13
db66a09dc0b6
parent 0
73bc20975514
child 14
578214a34ded
equal deleted inserted replaced
12:fb941ed514c6 13:db66a09dc0b6
1 local now = os.time;
1 2
2 module("memcache", package.seeall); 3 module("memcache", package.seeall);
4
5 local max_expires_seconds = 60*60*24*30;
3 6
4 function new() 7 function new()
5 local cache = {}; 8 local cache = {};
6 9
7 local _data = {}; 10 local _data = {};
8 local _flags = {}; 11 local _flags = {};
12 local _expiry = {};
13 local _deleted = {};
9 14
10 function cache:set(key, flags, expires, data) 15 function cache:set(key, flags, expires, data)
11 if expires ~= 0 then 16 if expires ~= 0 then
12 return false, "Expiry is not currently implemented"; 17 return false, "Expiry is not currently implemented";
13 end 18 end
18 23
19 function cache:get(key) 24 function cache:get(key)
20 return _flags[key], _data[key]; 25 return _flags[key], _data[key];
21 end 26 end
22 27
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
23 return cache; 40 return cache;
24 end 41 end
25 42
26 return _M; 43 return _M;

mercurial