]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blobdiff - www/webapp/handlers_iuse.py
fireinfo: Add new image.
[people/shoehn/ipfire.org.git] / www / webapp / handlers_iuse.py
index 97f5fac97b4e4a77c7cc8ee63062151e887f1e7f..e0b003a69668ffaa523ad9d0804796838fbaf457 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/python
 
+import logging
 import tornado.web
 
 from handlers_base import *
@@ -15,19 +16,34 @@ 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)
+               image = None
+               # Try to get the image from memcache. If we have a cache miss we
+               # build a new one.
+               mem_id = "iuse-%s-%s-%s" % (profile_id, image_id, self.locale.code)
 
-               profile = self.stasy.get_profile(profile_id)
-               if not profile:
-                       raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
+               cache = self.get_argument("cache", "true")
+               if cache == "true":
+                       image = self.memcached.get(mem_id)
 
-               self.set_header("Content-type", "image/png")
+               if image:
+                       logging.debug("Got image from cache for profile: %s" % profile_id)
+               else:
+                       logging.info("Rendering new image for profile: %s" % profile_id)
 
-               # Render the image
-               # XXX use memcached at this place
-               image = image_cls(profile)
+                       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.write(image.to_string())
+                       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(self, profile).to_string()
+
+                       # Save the image to the memcache for 15 minutes
+                       self.memcached.set(mem_id, image, 15*60)
+
+               self.set_header("Content-Type", "image/png")
+               self.write(image)