mooncached.lua

Sun, 01 Aug 2010 13:24:24 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 01 Aug 2010 13:24:24 +0100
changeset 19
61c4d7f8279c
parent 17
5bede08f2f55
permissions
-rw-r--r--

Add 'incr' and 'decr' commands (32-bit only)

0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local server = require "net.server";
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local log = require "util.logger".init("memcached");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local memcache = require "util.memcache";
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local cache = memcache.new();
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local memcached_listener = {};
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local command_handlers = {};
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 --- Network handlers
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function memcached_listener.onconnect(conn)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 function memcached_listener.onincoming(conn, line)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local command, params_pos = line:match("^(%S+) ?()");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local command_handler = command_handlers[command];
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if command_handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local ok, err = command_handler(conn, line:sub(params_pos));
2
c5215e30b6ef Assume that a command_handler returning nil succeeded
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
20 if ok == false then
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 conn:write("CLIENT_ERROR "..err.."\r\n");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 elseif command then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 log("warn", "Client sent unknown command: %s", command);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 conn:write("ERROR\r\n");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 function memcached_listener.ondisconnect(conn, err)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 --- Command handlers
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
17
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
34 local function generic_store_command(store_method, conn, params)
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local key, flags, exptime, bytes, reply = params:match("(%S+) (%d+) (%d+) (%d+) ?(.*)$");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 flags, exptime, bytes, reply = tonumber(flags), tonumber(exptime), tonumber(bytes), reply ~= "noreply";
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if not (flags and exptime and bytes) then
12
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
38 if reply then
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
39 return false, "Invalid parameter(s)";
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
40 else
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
41 return nil;
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
42 end
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 conn:set_mode("*a");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local received_count, received_buffer = 0, {};
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local function handle_data(conn, data)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 log("debug", "Received data of length "..#data.." out of "..bytes);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 received_count = received_count + #data;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 received_buffer[#received_buffer+1] = data;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if received_count >= bytes then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 received_buffer = table.concat(received_buffer);
17
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
52 local ok, err = store_method(cache, key, flags, exptime, received_buffer:sub(1,bytes));
12
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
53 if reply then
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
54 if ok then
17
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
55 if err == true then
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
56 conn:send("STORED\r\n");
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
57 else
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
58 conn:send("NOT_STORED\r\n");
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
59 end
12
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
60 else
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
61 conn:send("SERVER_ERROR "..(err or "Unknown error").."\r\n");
fb941ed514c6 Honour noreply flag for set command
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
62 end
5
a702c02caacc Report errors from the underlying memcache library on set
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
63 end
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 conn:setlistener(memcached_listener);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 conn:set_mode("*l");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if received_count > bytes then
3
aebc17f0d002 Print log message for re-handling extra bytes from a data block read
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
67 log("debug", "Re-handling %d extra bytes", received_count-bytes);
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 memcached_listener.onincoming(conn, received_buffer:sub(bytes+1));
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 conn:setlistener({
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 onincoming = handle_data;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 ondisconnect = memcached_listener.ondisconnect;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 });
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 log("debug", "Waiting for "..bytes.." bytes from client");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 return true;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
17
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
80 function command_handlers.set(conn, params)
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
81 return generic_store_command(cache.set, conn, params);
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
82 end
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
83
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
84 function command_handlers.add(conn, params)
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
85 return generic_store_command(cache.add, conn, params);
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
86 end
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
87
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
88 function command_handlers.replace(conn, params)
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
89 return generic_store_command(cache.replace, conn, params);
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
90 end
5bede08f2f55 Add 'add' and 'replace' commands
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
91
19
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
92 local function generic_increment_decrement_command(method, conn, params)
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
93 local key, amount = params:match("^(%S+) (%d+)");
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
94 local reply = params:match(" (noreply)$") ~= "noreply";
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
95 amount = tonumber(amount);
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
96 local ok, err;
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
97 if not (key and amount) then
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
98 if reply then
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
99 return false, "Invalid parameter(s)";
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
100 else
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
101 return nil;
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
102 end
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
103 end
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
104 local ok, new_value = method(cache, key, amount);
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
105 if ok and reply then
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
106 conn:write(new_value.."\r\n");
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
107 end
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
108 return true;
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
109 end
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
110
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
111 function command_handlers.incr(conn, params)
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
112 return generic_increment_decrement_command(cache.incr, conn, params);
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
113 end
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
114
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
115 function command_handlers.decr(conn, params)
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
116 return generic_increment_decrement_command(cache.decr, conn, params);
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
117 end
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
118
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 function command_handlers.get(conn, keys)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 for key in keys:gmatch("%S+") do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local flags, data = cache:get(key);
19
61c4d7f8279c Add 'incr' and 'decr' commands (32-bit only)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
122 if data then
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 conn:write("VALUE "..key.." "..flags.." "..#data.."\r\n"..data.."\r\n");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
1
1ec09b2f61c3 Send END after last get result
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
126 conn:write("END\r\n");
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return true;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
13
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
130 function command_handlers.delete(conn, params)
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
131 local key, keyend = params:match("^(%S+)()");
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
132 local time, reply = params:match(" (%d+)", keyend), params:match(" (noreply)$", keyend);
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
133 time, reply = tonumber(time), reply ~= "noreply";
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
134 local ok, err;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
135 if not key then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
136 ok, err = false, "Unable to determine key from request";
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
137 else
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
138 ok, err = cache:delete(key, time);
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
139 if ok then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
140 if err then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
141 conn:write("DELETED\r\n");
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
142 else
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
143 conn:write("NOT_FOUND\r\n");
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
144 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
145 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
146 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
147 if not reply then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
148 return nil;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
149 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
150 return ok, err;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
151 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
152
9
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
153 function command_handlers.version(conn)
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
154 conn:write("VERSION Mooncached 0.1\r\n");
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
155 return true;
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
156 end
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
157
4
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
158 function command_handlers.quit(conn)
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
159 conn:close();
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
160 return true;
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
161 end
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
162
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 logger.setwriter(function (name, level, format, ...) return print(name, level, format:format(...)); end);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 server.addserver("*", 11211, memcached_listener, "*l");
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 server.loop();

mercurial