memcached.lua

Sat, 31 Jul 2010 16:09:11 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 31 Jul 2010 16:09:11 +0100
changeset 0
73bc20975514
child 1
1ec09b2f61c3
permissions
-rw-r--r--

Memory is the storehouse in which the substance of our knowledge is treasured up.

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));
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 if not ok then
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);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 cache:set(key, flags, exptime, received_buffer:sub(1,bytes));
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 conn:send("STORED\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
50 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
51 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
52 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
53 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
54 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 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
58 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
59 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
60 });
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 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
62 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
63 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 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
66 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
67 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
68 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
69 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
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 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
73 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 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
76
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 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
78
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 server.loop();

mercurial