djmako / djmako / cache_plugin.py

"""
A cache plugin for mako that uses the django cache.
"""
from mako.cache import CacheImpl, register_plugin

_djcache = None

class DjangoCacheImpl(CacheImpl):
    """
    A mako cache plugin that calls the django cache.
    """
    
    def __init__(self, cache):
        super(DjangoCacheImpl, self).__init__(cache)
        global _djcache
        if _djcache is None:
            from django.core.cache import cache as _djcache
        self._djcache = _djcache
    
    def get_or_create(self, key, creation_function, **kw):
        djkey = 'djmako-' + key
        value = self._djcache.get(djkey)
        if value is None:
            value = creation_function()
            self._djcache.set(djkey, value)
        return value

    def set(self, key, value, **kwargs):
        self._djcache.set('djmako-' + key, value)

    def get(self, key, **kwargs):
        return self._djcache.get('djmako-' + key)

    def invalidate(self, key, **kwargs):
        self._djcache.remove('djmako-' + key)


register_plugin('djmakocache', __name__, 'DjangoCacheImpl')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.