Sat, 31 Jul 2010 19:15:27 +0100
memcached->mooncached
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 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | function command_handlers.set(conn, params) |
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 |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | return false, "Invalid parameter(s)"; |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | 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
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | 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
|
45 | 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
|
46 | 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
|
47 | received_buffer = table.concat(received_buffer); |
5
a702c02caacc
Report errors from the underlying memcache library on set
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
48 | local ok, err = cache:set(key, flags, exptime, received_buffer:sub(1,bytes)); |
a702c02caacc
Report errors from the underlying memcache library on set
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
49 | if ok then |
a702c02caacc
Report errors from the underlying memcache library on set
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
50 | conn:send("STORED\r\n"); |
a702c02caacc
Report errors from the underlying memcache library on set
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
51 | else |
a702c02caacc
Report errors from the underlying memcache library on set
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
52 | conn:send("SERVER_ERROR "..(err or "Unknown error").."\r\n"); |
a702c02caacc
Report errors from the underlying memcache library on set
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
53 | 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
|
54 | 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
|
55 | 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
|
56 | 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
|
57 | 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
|
58 | 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
|
59 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | 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
|
63 | 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
|
64 | 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
|
65 | }); |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | 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
|
67 | 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
|
68 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | 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
|
71 | 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
|
72 | 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
|
73 | 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
|
74 | 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
|
75 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | end |
1
1ec09b2f61c3
Send END after last get result
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
77 | 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
|
78 | 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
|
79 | end |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | |
4 | 81 | function command_handlers.quit(conn) |
82 | conn:close(); | |
83 | return true; | |
84 | end | |
85 | ||
0
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | 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
|
87 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | 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
|
89 | |
73bc20975514
Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | server.loop(); |