util/memcache.lua

changeset 19
61c4d7f8279c
parent 17
5bede08f2f55
--- a/util/memcache.lua	Sun Aug 01 12:41:10 2010 +0100
+++ b/util/memcache.lua	Sun Aug 01 13:24:24 2010 +0100
@@ -48,7 +48,7 @@
 		if expires and expires < now() then
 			self:delete(key);
 		end
-		return _flags[key], _data[key];
+		return _flags[key], tostring(_data[key]);
 	end
 	
 	function cache:delete(key, time)
@@ -63,6 +63,26 @@
 		return true, existed;
 	end
 	
+	function cache:incr(key, amount)
+		local current_value = tonumber((select(2, self:get(key)))) or 0;
+		local new_value = (current_value + amount)%4294967296;
+		local ok, err = cache:set(key, 0, 0, new_value);
+		if not ok then
+			return ok, err;
+		end
+		return ok, new_value;
+	end
+	
+	function cache:decr(key, amount)
+		local current_value = tonumber((select(2, self:get(key)))) or 0;
+		local new_value = math.max(0, current_value - amount);
+		local ok, err = cache:set(key, 0, 0, new_value);
+		if not ok then
+			return ok, err;
+		end
+		return ok, new_value;
+	end
+	
 	return cache;
 end
 

mercurial