util/memcache.lua

changeset 19
61c4d7f8279c
parent 17
5bede08f2f55
equal deleted inserted replaced
18:d4822ae9d901 19:61c4d7f8279c
46 function cache:get(key) 46 function cache:get(key)
47 local expires = _expiry[key]; 47 local expires = _expiry[key];
48 if expires and expires < now() then 48 if expires and expires < now() then
49 self:delete(key); 49 self:delete(key);
50 end 50 end
51 return _flags[key], _data[key]; 51 return _flags[key], tostring(_data[key]);
52 end 52 end
53 53
54 function cache:delete(key, time) 54 function cache:delete(key, time)
55 local existed = _data[key]; 55 local existed = _data[key];
56 _flags[key], _data[key], _expiry[key] = nil, nil, nil; 56 _flags[key], _data[key], _expiry[key] = nil, nil, nil;
61 _deleted[key] = time; 61 _deleted[key] = time;
62 end 62 end
63 return true, existed; 63 return true, existed;
64 end 64 end
65 65
66 function cache:incr(key, amount)
67 local current_value = tonumber((select(2, self:get(key)))) or 0;
68 local new_value = (current_value + amount)%4294967296;
69 local ok, err = cache:set(key, 0, 0, new_value);
70 if not ok then
71 return ok, err;
72 end
73 return ok, new_value;
74 end
75
76 function cache:decr(key, amount)
77 local current_value = tonumber((select(2, self:get(key)))) or 0;
78 local new_value = math.max(0, current_value - amount);
79 local ok, err = cache:set(key, 0, 0, new_value);
80 if not ok then
81 return ok, err;
82 end
83 return ok, new_value;
84 end
85
66 return cache; 86 return cache;
67 end 87 end
68 88
69 return _M; 89 return _M;

mercurial