Add 'delete' command

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 12
fb941ed514c6
child 14
578214a34ded

Add 'delete' command

mooncached.lua file | annotate | diff | comparison | revisions
util/memcache.lua file | annotate | diff | comparison | revisions
--- a/mooncached.lua	Sun Aug 01 12:16:06 2010 +0100
+++ b/mooncached.lua	Sun Aug 01 12:17:12 2010 +0100
@@ -84,6 +84,29 @@
 	return true;
 end
 
+function command_handlers.delete(conn, params)
+	local key, keyend = params:match("^(%S+)()");
+	local time, reply = params:match(" (%d+)", keyend), params:match(" (noreply)$", keyend);
+	time, reply = tonumber(time), reply ~= "noreply";
+	local ok, err;
+	if not key then
+		ok, err = false, "Unable to determine key from request";
+	else
+		ok, err = cache:delete(key, time);
+		if ok then
+			if err then
+				conn:write("DELETED\r\n");
+			else
+				conn:write("NOT_FOUND\r\n");
+			end
+		end
+	end
+	if not reply then
+		return nil;
+	end
+	return ok, err;
+end
+
 function command_handlers.version(conn)
 	conn:write("VERSION Mooncached 0.1\r\n");
 	return true;
--- a/util/memcache.lua	Sun Aug 01 12:16:06 2010 +0100
+++ b/util/memcache.lua	Sun Aug 01 12:17:12 2010 +0100
@@ -1,11 +1,16 @@
+local now = os.time;
 
 module("memcache", package.seeall);
 
+local max_expires_seconds = 60*60*24*30;
+
 function new()
 	local cache = {};
 	
 	local _data = {};
 	local _flags = {};
+	local _expiry = {};
+	local _deleted = {};
 	
 	function cache:set(key, flags, expires, data)
 		if expires ~= 0 then
@@ -20,6 +25,18 @@
 		return _flags[key], _data[key];
 	end
 	
+	function cache:delete(key, time)
+		local existed = _data[key];
+		_flags[key], _data[key], _expires[key] = nil, nil, nil;
+		if existed and time then
+			if time <= max_expires_seconds then
+				time = now() + time;
+			end
+			_deleted[key] = time;
+		end
+		return true, existed;
+	end
+	
 	return cache;
 end
 

mercurial