mooncached.lua

Sun, 01 Aug 2010 12:40:45 +0100

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

Add 'add' and 'replace' commands

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
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 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
93 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
94 local flags, data = cache:get(key);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 if flags then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 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
97 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
1
1ec09b2f61c3 Send END after last get result
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
99 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
100 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
101 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
13
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
103 function command_handlers.delete(conn, params)
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
104 local key, keyend = params:match("^(%S+)()");
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
105 local time, reply = params:match(" (%d+)", keyend), params:match(" (noreply)$", keyend);
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
106 time, reply = tonumber(time), reply ~= "noreply";
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
107 local ok, err;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
108 if not key then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
109 ok, err = false, "Unable to determine key from request";
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
110 else
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
111 ok, err = cache:delete(key, time);
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
112 if ok then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
113 if err then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
114 conn:write("DELETED\r\n");
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
115 else
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
116 conn:write("NOT_FOUND\r\n");
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
117 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
118 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
119 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
120 if not reply then
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
121 return nil;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
122 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
123 return ok, err;
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
124 end
db66a09dc0b6 Add 'delete' command
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
125
9
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
126 function command_handlers.version(conn)
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
127 conn:write("VERSION Mooncached 0.1\r\n");
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
128 return true;
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
129 end
ff6a712c26c8 Add version command handler
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
130
4
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
131 function command_handlers.quit(conn)
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
132 conn:close();
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
133 return true;
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
134 end
31e1af4738cb Add 'quit' command
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
135
0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 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
137
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 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
139
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 server.loop();

mercurial