django-utils v0.3.0 documentation

This Page

Cache

djutils.cache.cached_filter(func, timeout=300)

Decorator for creating a cached template filter.

Usage:

@register.filter
@cached_filter
def expensive_filter(value):
    ... do something expensive
class djutils.cache.CachedNode(template.Node)

Base class for creating cached template Nodes. This class is designed to not dogpile in the event a cache key expires. Suppose the data takes 3 seconds to calculate and you get 100 requests/s, that’s ~300 hits. This class provides several ways to avoid this problem, either by blocking until the cache has been repopulated or by return an empty result.

To avoid dogpiling the time the cached data expires is stored along with the cached data and if it is nearing expiration, a single call is made that will repopulate the data (hopefully before it expires).

There are a number of ways you can configure the operation of this class:

cache_timeout = 60

The default time to store data in the cache

aggressive = True

Whether or not to block when cache is populating

use_spin_lock = False

Whether to wait on repopulating call or fire off another call to the get_content method

backoff = (0.1, 1.1, 1.0)

If using a spin-lock, initial time to sleep, rate of backoff, maximum

get_cache_key(self, context)

return a unique cache key based on the available context

get_content(self, context)

return any content – what you would normally return in render()

class djutils.cache.CachedContextNode(CachedNode)

Similar to CachedNode except that the get_content() method returns a dictionary of keys to update in the template context (as opposed to a block of text to render)