From: Michael Tremer Date: Sun, 19 Apr 2015 19:32:38 +0000 (+0200) Subject: nopaste: Use memcache to cache the content of the pastes X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0de07bc3b4d19ff35bc8bd85665b104676b54456;p=ipfire.org.git nopaste: Use memcache to cache the content of the pastes --- diff --git a/templates/nopaste/view.html b/templates/nopaste/view.html index d604ec41..54aa030c 100644 --- a/templates/nopaste/view.html +++ b/templates/nopaste/view.html @@ -7,8 +7,8 @@

{{ entry.subject or _("Paste %s") % entry.uuid }}

- {% if entry.mimetype.startswith("text/") %} -
{{ entry.content }}
+ {% if content %} +
{{ content }}
diff --git a/webapp/backend/nopaste.py b/webapp/backend/nopaste.py index c68d9707..7db578a3 100644 --- a/webapp/backend/nopaste.py +++ b/webapp/backend/nopaste.py @@ -47,13 +47,11 @@ class Nopaste(Object): ms.close() def get(self, uuid): - res = self.db.get("SELECT * FROM nopaste WHERE uuid = %s AND \ - (CASE WHEN time_expires IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid) + res = self.db.get("SELECT uuid, subject, time_created, time_expires, address, uid, \ + mimetype, views, size FROM nopaste WHERE uuid = %s AND (CASE WHEN time_expires \ + IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid) if res: - # Convert the content to a byte string - res.content = "%s" % res.content - # Get the account that uploaded this if available res.account = None if res.uid: @@ -64,6 +62,22 @@ class Nopaste(Object): return res + def get_content(self, uuid): + # Try fetching the object from memcache. If found, we return it right away. + content = self.memcache.get("nopaste-%s" % uuid) + + # If the object was not in the cache, we need to fetch it from the database. + if not content: + res = self.db.get("SELECT content FROM nopaste WHERE uuid = %s", uuid) + + # Convert the content to a byte string + content = "%s" % res.content + + # Save it in the cache for later + self.memcache.set("nopaste-%s" % uuid, content, 6 * 3600) + + return content + def _update_lastseen(self, uuid): self.db.execute("UPDATE nopaste SET time_lastseen = NOW(), views = views + 1 \ WHERE uuid = %s", uuid) diff --git a/webapp/handlers_nopaste.py b/webapp/handlers_nopaste.py index 5199b9c5..0a8715a5 100644 --- a/webapp/handlers_nopaste.py +++ b/webapp/handlers_nopaste.py @@ -73,7 +73,8 @@ class NopasteRawHandler(BaseHandler): self.set_header("Content-Type", entry.mimetype) # Send content - self.finish(entry.content) + content = self.backend.nopaste.get_content(entry.uuid) + self.finish(content) class NopasteViewHandler(BaseHandler): @@ -82,4 +83,10 @@ class NopasteViewHandler(BaseHandler): if not entry: raise tornado.web.HTTPError(404) - self.render("nopaste/view.html", entry=entry) + # Fetch the content if the output should be displayed + if entry.mimetype.startswith("text/"): + content = self.backend.nopaste.get_content(entry.uuid) + else: + content = None + + self.render("nopaste/view.html", entry=entry, content=content)