Skip to content

Instantly share code, notes, and snippets.

@fiorix
Created August 30, 2012 03:03
Show Gist options
  • Save fiorix/3521927 to your computer and use it in GitHub Desktop.
Save fiorix/3521927 to your computer and use it in GitHub Desktop.
twisted memcache client on cyclone web server
# coding: utf-8
# twistd -n cyclone -r test.Application
import cyclone.web
from twisted.internet import defer
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.protocols.memcache import MemCacheProtocol
class MemcacheMixin(object):
memcache = None
@classmethod
def memcache_setup(cls, path="/tmp/memcache"):
c = protocol.ClientCreator(reactor, MemCacheProtocol)
c.connectUNIX(path).addCallback(lambda p: setattr(cls, "memcache", p))
class IndexHandler(cyclone.web.RequestHandler, MemcacheMixin):
@defer.inlineCallbacks
def get(self):
v = yield self.memcache.get("test")
self.write("test=%s\r\n" % repr(v))
@defer.inlineCallbacks
def post(self):
v = self.get_argument("v").encode("utf-8")
yield self.memcache.set("test", v)
self.write("ok\r\n")
class Application(cyclone.web.Application):
def __init__(self):
handlers = [ (r"/", IndexHandler) ]
settings = {"debug": True}
MemcacheMixin.memcache_setup()
cyclone.web.Application.__init__(self, handlers, **settings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment