Skip to content

Instantly share code, notes, and snippets.

@puentesarrin
Created June 14, 2012 00:09
Show Gist options
  • Save puentesarrin/2927255 to your computer and use it in GitHub Desktop.
Save puentesarrin/2927255 to your computer and use it in GitHub Desktop.
Simple cache for Tornado Handler using MongoDB
class CacheBaseHandler(tornado.web.RequestHandler):
def prepare(self):
cached = self.application.db.cache.find_one({"slug": self.request.path})
if cached is not None:
self.write(cached["content"])
self.finish()
def render_string(self, template_name, **kwargs):
html_generated = \
super(CacheBaseHandler, self).render_string(template_name, **kwargs)
self.application.db.cache.update({"slug": self.request.path},
{"$set": {"content": html_generated}},
upsert=True)
return html_generated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment