]> git.ipfire.org Git - ipfire.org.git/commitdiff
nopaste: Use memcache to cache the content of the pastes
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 19 Apr 2015 19:32:38 +0000 (21:32 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 19 Apr 2015 19:32:38 +0000 (21:32 +0200)
templates/nopaste/view.html
webapp/backend/nopaste.py
webapp/handlers_nopaste.py

index d604ec415ee9a05dc6a22b585e3fdba2253a282e..54aa030c5816cb9701bb2da7be133e798dd8331b 100644 (file)
@@ -7,8 +7,8 @@
                <h3>{{ entry.subject or _("Paste %s") % entry.uuid }}</h3>
        </div>
 
-       {% if entry.mimetype.startswith("text/") %}
-               <pre class="prettyprint linenums:4" style="min-height: 25em">{{ entry.content }}</pre>
+       {% if content %}
+               <pre class="prettyprint linenums:4" style="min-height: 25em">{{ content }}</pre>
 
                <link rel="stylesheet" href="{{ static_url("css/prettify.css") }}">
                <script src="{{ static_url("js/prettify.js") }}"></script>
index c68d97079c6e53a1ecf479364a4549670333d125..7db578a3ddff3174482b1d14778e7c7cec9e08e3 100644 (file)
@@ -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)
index 5199b9c5f6a0e12592235cd68d3d3011fc4b327e..0a8715a54f14af75945cd5d44dba08500500e1cd 100644 (file)
@@ -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)