mooncached.lua

changeset 19
61c4d7f8279c
parent 17
5bede08f2f55
equal deleted inserted replaced
18:d4822ae9d901 19:61c4d7f8279c
87 87
88 function command_handlers.replace(conn, params) 88 function command_handlers.replace(conn, params)
89 return generic_store_command(cache.replace, conn, params); 89 return generic_store_command(cache.replace, conn, params);
90 end 90 end
91 91
92 local function generic_increment_decrement_command(method, conn, params)
93 local key, amount = params:match("^(%S+) (%d+)");
94 local reply = params:match(" (noreply)$") ~= "noreply";
95 amount = tonumber(amount);
96 local ok, err;
97 if not (key and amount) then
98 if reply then
99 return false, "Invalid parameter(s)";
100 else
101 return nil;
102 end
103 end
104 local ok, new_value = method(cache, key, amount);
105 if ok and reply then
106 conn:write(new_value.."\r\n");
107 end
108 return true;
109 end
110
111 function command_handlers.incr(conn, params)
112 return generic_increment_decrement_command(cache.incr, conn, params);
113 end
114
115 function command_handlers.decr(conn, params)
116 return generic_increment_decrement_command(cache.decr, conn, params);
117 end
118
92 function command_handlers.get(conn, keys) 119 function command_handlers.get(conn, keys)
93 for key in keys:gmatch("%S+") do 120 for key in keys:gmatch("%S+") do
94 local flags, data = cache:get(key); 121 local flags, data = cache:get(key);
95 if flags then 122 if data then
96 conn:write("VALUE "..key.." "..flags.." "..#data.."\r\n"..data.."\r\n"); 123 conn:write("VALUE "..key.." "..flags.." "..#data.."\r\n"..data.."\r\n");
97 end 124 end
98 end 125 end
99 conn:write("END\r\n"); 126 conn:write("END\r\n");
100 return true; 127 return true;

mercurial