Commits

Kai Diefenbach committed 6b17eb1

Added caching to slots_information

Comments (0)

Files changed (1)

lfstheme/templatetags/lfs_theme_tags.py

+# django imports
+from django.core.cache import cache
 from django.template import Library, Node, TemplateSyntaxError
 from django.utils.translation import ugettext as _
 
         if object is None:
             object = lfs.core.utils.get_default_shop()
 
-        for slot in Slot.objects.all():
-            context["Slot%s" % slot.name] = portlets.utils.has_portlets(slot, object)
+        slots = cache.get("slots")
+        if slots is None:
+            slots = Slot.objects.all()
+            cache.set("slots", slots)
 
-        content_class = "span-24 last"
-        if context.get("SlotLeft", None) and context.get("SlotRight", None):
-            content_class = "span-15 padding-both"
-        elif context.get("SlotLeft", None):
-            content_class = "span-19 padding-left last"
-        elif context.get("SlotRight", None):
-            content_class = "span-20 padding-right"
+        for slot in slots:
+            cache_key = "has-portlets-%s-%s-%s" % (object.__class__.__name__, object.id, slot.name)
+            has_portlets = cache.get(cache_key)
+            if has_portlets is None:
+                has_portlets = portlets.utils.has_portlets(slot, object)
+                cache.set(cache_key, has_portlets)
+
+            context["Slot%s" % slot.name] = has_portlets
+
+        cache_key = "content-class-%s-%s" % (object.__class__.__name__, object.id)
+        content_class = cache.get(cache_key)
+        if content_class is None:
+            content_class = "span-24 last"
+            if context.get("SlotLeft", None) and context.get("SlotRight", None):
+                content_class = "span-15 padding-both"
+            elif context.get("SlotLeft", None):
+                content_class = "span-19 padding-left last"
+            elif context.get("SlotRight", None):
+                content_class = "span-20 padding-right"
+
+            cache.set(cache_key, content_class)
 
         context["content_class"] = content_class
         return ''