-
Notifications
You must be signed in to change notification settings - Fork 32
/
memg-diesel.py
38 lines (29 loc) · 866 Bytes
/
memg-diesel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
from diesel import Service, quickstart, until_eol, send, receive
CACHE = {}
def handle_con(unused):
while True:
line = until_eol()
if line == "":
break
parts = line.split()
cmd = parts[0]
if cmd == "get":
key = parts[1]
try:
val = CACHE[key]
send("VALUE %s 0 %d\r\n" % (key, len(val)))
send(val + "\r\n")
except KeyError:
pass
send("END\r\n")
elif cmd == "set":
key = parts[1]
#exp = parts[2]
#flags = parts[3]
length = int(parts[4])
val = receive(length + 2)[:length]
CACHE[key] = val
send("STORED\r\n")
if __name__ == "__main__":
quickstart(Service(handle_con, 11211))