Login

template + cache = crazy delicious

Author:
jacobian
Posted:
May 9, 2007
Language:
Python
Version:
.96
Tags:
tag templatetag cache
Score:
7 (after 7 ratings)

A couple of utility Node subclasses that will automatically cache thier contents.

Use CachedNode for template tags that output content into the template:

class SomeNode(CachedNode):

    def get_cache_key(self, context):
        return "some-cache-key"

    def get_content(self, context):
        return expensive_operation()

Use CachedContextUpdatingNode for tags that update the context:

class AnotherNode(CachedContextUpdatingNode):

    # Only cache for 60 seconds
    cache_timeout = 60

    def get_cache_key(self, context);
        return "some-other-cache-key"

    def get_content(self, context):
        return {"key" : expensive_operation()}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from django.core import template
from django.core.cache import cache
from django.conf import settings

class CachedNode(template.Node):
    """
    Cached template node.  
    
    Subclasses should define the methods ``get_cache_key()`` and
    ``get_content()`` instead of the standard render() method. Subclasses may
    also define the class attribute ``cache_timeout`` to override the default
    cache timeout of ten minutes.
    """
    
    cache_timeout = 600
    
    def render(self, context):
        if settings.DEBUG:
            return self.get_content(context)
        key = self.get_cache_key(context)
        content = cache.get(key)
        if not content:
            content = self.get_content(context)
            cache.set(key, content, self.cache_timeout)
        return content
        
    def get_cache_key(self, context):
        raise NotImplementedError()
        
    def get_content(self, context):
        raise NotImplementedError()
        
class ContextUpdatingNode(template.Node):
    """
    Node that updates the context with certain values.
    
    Subclasses should define ``get_content()``, which should return a dictionary
    to be added to the context.
    """
    
    def render(self, context):
        context.update(self.get_content(context))
        return ''
        
class CachedContextUpdatingNode(CachedNode, ContextUpdatingNode):
    """
    Node that updates the context, and is cached. Subclasses need to define
    ``get_cache_key()`` and ``get_content()``.
    """
    
    def render(self, context):
        context.update(CachedNode.render(self, context))
        return ''

More like this

  1. Function decorator for caching function results in local memory by bikeshedder 2 years, 6 months ago
  2. Run and cache only one instance of a heavy request by farnsworth 4 years, 7 months ago
  3. Method Caching by bryanhelmig 3 years, 9 months ago
  4. CatchTag - Catching the content and saving it to a variable by limodou 8 years, 1 month ago
  5. Automatic Memoization Decorator by nikmolnar 2 years, 2 months ago

Comments

ubernostrum (on May 10, 2007):

This looks familiar... :)

#

stormlifter (on January 3, 2011):

What cache does this use? Doesn't seem to be using the default cache in 1.2.4

#

Please login first before commenting.