]> git.ipfire.org Git - ipfire.org.git/commitdiff
Add support for memcached.
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Dec 2010 01:21:59 +0000 (02:21 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Dec 2010 01:21:59 +0000 (02:21 +0100)
www/webapp/backend/__init__.py
www/webapp/backend/memcached.py [new file with mode: 0644]
www/webapp/handlers_base.py
www/webapp/handlers_iuse.py

index 390783e6b11f5d83532bc4a597fd846a0e4399b8..83ecfec134aa795d94ec2c7eea8ba33a191c08f3 100644 (file)
@@ -4,6 +4,7 @@ from accounts   import Accounts
 from banners   import Banners
 from geoip             import GeoIP
 from iuse              import IUse
+from memcached import Memcached
 from menu              import Menu
 from mirrors   import Mirrors
 from netboot   import NetBoot
diff --git a/www/webapp/backend/memcached.py b/www/webapp/backend/memcached.py
new file mode 100644 (file)
index 0000000..bf293d5
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/python
+
+import memcache
+
+from misc import Singleton
+from settings import Settings
+
+class Memcached(object):
+       __metaclass__ = Singleton
+
+       def __init__(self):
+               # Fetch hosts from database
+               hosts = Settings().get("memcached_servers").split(",")
+
+               self._connection = memcache.Client(hosts, debug=0)
+
+       def get(self, *args, **kwargs):
+               return self._connection.get(*args, **kwargs)
+
+       def set(self, *args, **kwargs):
+               return self._connection.set(*args, **kwargs)
index 21ded7e437415c491335d46cdb363b12d50095d8..602b9fef49317d5a4728b613895174a91c80aba5 100644 (file)
@@ -68,6 +68,10 @@ class BaseHandler(tornado.web.RequestHandler):
        def banners(self):
                return backend.Banners()
 
+       @property
+       def memcached(self):
+               return backend.Memcached()
+
        @property
        def mirrors(self):
                return backend.Mirrors()
index 97f5fac97b4e4a77c7cc8ee63062151e887f1e7f..b7a66f29b2dbca774bfb16a9755eee8ce7fbc372 100644 (file)
@@ -15,19 +15,26 @@ class IUseImage(BaseHandler):
                return backend.Stasy()
 
        def get(self, profile_id, image_id):
-               image_cls = self.iuse.get_imagetype(image_id)
-               if not image_cls:
-                       raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id)
+               # Try to get the image from memcache. If we have a cache miss we
+               # build a new one.
+               mem_id = "iuse-%s-%s" % (profile_id, image_id)
+               image = self.memcached.get(mem_id)
 
-               profile = self.stasy.get_profile(profile_id)
-               if not profile:
-                       raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
+               if not image:
+                       image_cls = self.iuse.get_imagetype(image_id)
+                       if not image_cls:
+                               raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id)
 
-               self.set_header("Content-type", "image/png")
+                       profile = self.stasy.get_profile(profile_id)
+                       if not profile:
+                               raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
+
+                       # Render the image
+                       image = image_cls(profile).to_string()
 
-               # Render the image
-               # XXX use memcached at this place
-               image = image_cls(profile)
+                       # Save the image to the memcache for 15 minutes
+                       self.memcached.set(mem_id, image, 15*60)
 
-               self.write(image.to_string())
+               self.set_header("Content-type", "image/png")
+               self.write(image)