Skip to content

Instantly share code, notes, and snippets.

@smartkiwi
Created July 21, 2015 03:51
Show Gist options
  • Save smartkiwi/aa3b648d996651dca0e4 to your computer and use it in GitHub Desktop.
Save smartkiwi/aa3b648d996651dca0e4 to your computer and use it in GitHub Desktop.
Twisted Web App that access mongo and memcached
#!/usr/bin/env python
# coding: utf-8
# Copyright 2009 Alexandre Fiori
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# requires cyclone:
# http://github.com/fiorix/cyclone
# run:
# twistd -ny cyclone_server.tac
import _local_path
import txmongo
import cyclone.web
import cyclone.xmlrpc
from twisted.internet import defer
from twisted.application import service, internet
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.connectTCP("localhost", 11211).addCallback(lambda p: setattr(cls, "memcache", p))
class IndexHandler(cyclone.web.RequestHandler, MemcacheMixin):
@defer.inlineCallbacks
def get(self):
name = self.get_argument("name")
try:
result = yield self.settings.db.find({"name":name})
result += yield self.memcache.get(name.encode("utf-8"))
except Exception, e:
self.write("find failed: %s\n" % str(e))
else:
self.write("result(s): %s\n" % repr(result))
self.finish()
@defer.inlineCallbacks
def post(self):
name = self.get_argument("name")
v = self.get_argument("name").encode("utf-8")
self.settings.db.insert({"name":name})
yield self.memcache.set("name", v)
self.write("ok\n")
class XmlrpcHandler(cyclone.xmlrpc.XmlrpcRequestHandler):
@defer.inlineCallbacks
@cyclone.web.asynchronous
def xmlrpc_find(self, spec, limit=10):
result = yield self.settings.db.find(spec, limit=limit)
defer.returnValue(repr(result))
@defer.inlineCallbacks
@cyclone.web.asynchronous
def xmlrpc_insert(self, doc):
result = yield self.settings.db.insert(doc, safe=True)
defer.returnValue(repr(result))
@defer.inlineCallbacks
@cyclone.web.asynchronous
def xmlrpc_update(self, spec, doc):
result = yield self.settings.db.update(spec, doc, safe=True)
defer.returnValue(repr(result))
class WebMongo(cyclone.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler),
(r"/xmlrpc", XmlrpcHandler),
]
mongo = txmongo.lazyMongoConnectionPool()
settings = {
"db": mongo.test_database.test_collection,
"debug": True,
#"static_path": "./static",
}
MemcacheMixin.memcache_setup()
cyclone.web.Application.__init__(self, handlers, **settings)
application = service.Application("webmongo")
srv = internet.TCPServer(8888, WebMongo(), interface="127.0.0.1")
srv.setServiceParent(application)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment