Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented more stuff in the example.

  • Loading branch information...
commit 3b9ad830e186336e99afe8b32fb15a32e0f4ff61 1 parent b0fa771
@dustin authored
Showing with 52 additions and 0 deletions.
  1. +42 −0 examples/server.py
  2. +10 −0 memcache/binary.py
View
42 examples/server.py
@@ -37,14 +37,46 @@ def doGet(self, req, data):
else:
return res
+ def doGetQ(self, req, data):
+ try:
+ return self.doGet(req, data)
+ except binary.MemcachedNotFound:
+ return binary.EmptyResponse()
+
def doSet(self, req, data):
flags, exp = struct.unpack(constants.SET_PKT_FMT, req.extra)
self.d[req.key] = (exp, flags, 0, data)
+ def doAdd(self, req, data):
+ if req.key in self.d:
+ raise binary.MemcachedExists()
+ else:
+ flags, exp = struct.unpack(constants.SET_PKT_FMT, req.extra)
+ self.d[req.key] = (exp, flags, 0, data)
+
+ @_requireKey
+ def doAppend(self, req, newdata):
+ exp, flags, cas, olddata = self.d[req.key]
+ self.d[req.key] = (exp, flags, 0, olddata + newdata)
+
+ @_requireKey
+ def doPrepend(self, req, newdata):
+ exp, flags, cas, olddata = self.d[req.key]
+ self.d[req.key] = (exp, flags, 0, newdata + olddata)
+
@_requireKey
def doDelete(self, req, data):
del self.d[req.key]
+ def doFlush(self, req, data):
+ self.d = {}
+
+ def doStats(self, req, data):
+ r = binary.MultiResponse()
+ r.add(binary.Response(req, key='version', data='blah'))
+ r.add(binary.Response(req, key='', data=''))
+ return r
+
storage = DictStorage()
def ex(*a):
@@ -53,12 +85,22 @@ def ex(*a):
# this also works, but apparently confuses people.
# sys.exit(0)
+def doNoop(req, data):
+ return binary.Response(req)
+
class ExampleBinaryServer(binary.BinaryServerProtocol):
handlers = {
constants.CMD_GET: storage.doGet,
+ constants.CMD_GETQ: storage.doGetQ,
constants.CMD_SET: storage.doSet,
+ constants.CMD_ADD: storage.doAdd,
+ constants.CMD_APPEND: storage.doAppend,
+ constants.CMD_PREPEND: storage.doPrepend,
constants.CMD_DELETE: storage.doDelete,
+ constants.CMD_STAT: storage.doStats,
+ constants.CMD_FLUSH: storage.doFlush,
+ constants.CMD_NOOP: doNoop,
constants.CMD_QUIT: ex
}
View
10 memcache/binary.py
@@ -44,6 +44,16 @@ def toSequence(self):
res.cas)
return [hdr, self.key, self.extra, self.data]
+class MultiResponse(list):
+ def add(self, r):
+ self.extend(r.toSequence())
+ def toSequence(self):
+ return self
+
+class EmptyResponse(object):
+ def toSequence(self):
+ return []
+
class GetResponse(Response):
def __init__(self, req, flags, cas=0, status=0, key='', data=''):
Please sign in to comment.
Something went wrong with that request. Please try again.