diff -r d4822ae9d901 -r 61c4d7f8279c mooncached.lua --- a/mooncached.lua Sun Aug 01 12:41:10 2010 +0100 +++ b/mooncached.lua Sun Aug 01 13:24:24 2010 +0100 @@ -89,10 +89,37 @@ return generic_store_command(cache.replace, conn, params); end +local function generic_increment_decrement_command(method, conn, params) + local key, amount = params:match("^(%S+) (%d+)"); + local reply = params:match(" (noreply)$") ~= "noreply"; + amount = tonumber(amount); + local ok, err; + if not (key and amount) then + if reply then + return false, "Invalid parameter(s)"; + else + return nil; + end + end + local ok, new_value = method(cache, key, amount); + if ok and reply then + conn:write(new_value.."\r\n"); + end + return true; +end + +function command_handlers.incr(conn, params) + return generic_increment_decrement_command(cache.incr, conn, params); +end + +function command_handlers.decr(conn, params) + return generic_increment_decrement_command(cache.decr, conn, params); +end + function command_handlers.get(conn, keys) for key in keys:gmatch("%S+") do local flags, data = cache:get(key); - if flags then + if data then conn:write("VALUE "..key.." "..flags.." "..#data.."\r\n"..data.."\r\n"); end end