Created
June 14, 2012 00:09
-
-
Save puentesarrin/2927255 to your computer and use it in GitHub Desktop.
Simple cache for Tornado Handler using MongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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