mooncached.lua

changeset 12
fb941ed514c6
parent 9
ff6a712c26c8
child 13
db66a09dc0b6
equal deleted inserted replaced
11:85c18da60925 12:fb941ed514c6
33 33
34 function command_handlers.set(conn, params) 34 function command_handlers.set(conn, params)
35 local key, flags, exptime, bytes, reply = params:match("(%S+) (%d+) (%d+) (%d+) ?(.*)$"); 35 local key, flags, exptime, bytes, reply = params:match("(%S+) (%d+) (%d+) (%d+) ?(.*)$");
36 flags, exptime, bytes, reply = tonumber(flags), tonumber(exptime), tonumber(bytes), reply ~= "noreply"; 36 flags, exptime, bytes, reply = tonumber(flags), tonumber(exptime), tonumber(bytes), reply ~= "noreply";
37 if not (flags and exptime and bytes) then 37 if not (flags and exptime and bytes) then
38 return false, "Invalid parameter(s)"; 38 if reply then
39 return false, "Invalid parameter(s)";
40 else
41 return nil;
42 end
39 end 43 end
40 conn:set_mode("*a"); 44 conn:set_mode("*a");
41 local received_count, received_buffer = 0, {}; 45 local received_count, received_buffer = 0, {};
42 local function handle_data(conn, data) 46 local function handle_data(conn, data)
43 log("debug", "Received data of length "..#data.." out of "..bytes); 47 log("debug", "Received data of length "..#data.." out of "..bytes);
44 received_count = received_count + #data; 48 received_count = received_count + #data;
45 received_buffer[#received_buffer+1] = data; 49 received_buffer[#received_buffer+1] = data;
46 if received_count >= bytes then 50 if received_count >= bytes then
47 received_buffer = table.concat(received_buffer); 51 received_buffer = table.concat(received_buffer);
48 local ok, err = cache:set(key, flags, exptime, received_buffer:sub(1,bytes)); 52 local ok, err = cache:set(key, flags, exptime, received_buffer:sub(1,bytes));
49 if ok then 53 if reply then
50 conn:send("STORED\r\n"); 54 if ok then
51 else 55 conn:send("STORED\r\n");
52 conn:send("SERVER_ERROR "..(err or "Unknown error").."\r\n"); 56 else
57 conn:send("SERVER_ERROR "..(err or "Unknown error").."\r\n");
58 end
53 end 59 end
54 conn:setlistener(memcached_listener); 60 conn:setlistener(memcached_listener);
55 conn:set_mode("*l"); 61 conn:set_mode("*l");
56 if received_count > bytes then 62 if received_count > bytes then
57 log("debug", "Re-handling %d extra bytes", received_count-bytes); 63 log("debug", "Re-handling %d extra bytes", received_count-bytes);

mercurial