dogpile.cache / dogpile / cache / plugins /

Mako Integration

dogpile.cache includes a `Mako <>`_ plugin that replaces `Beaker <>`_ 
as the cache backend.
Setup a Mako template lookup using the "dogpile.cache" cache implementation
and a region dictionary::

    from dogpile.cache import make_region
    from mako.lookup import TemplateLookup

    my_regions = {

    mako_lookup = TemplateLookup(

To use the above configuration in a template, use the ``cached=True`` argument on any
Mako tag which accepts it, in conjunction with the name of the desired region
as the ``cache_region`` argument::

    <%def name="mysection()" cached=True cache_region="memcached">
        some content that's cached

from mako.cache import CacheImpl

class MakoPlugin(CacheImpl):
    """A Mako ``CacheImpl`` which talks to dogpile.cache."""

    def __init__(self, cache):
        super(MakoPlugin, self).__init__(cache)
            self.regions = self.cache.template.cache_args['regions']
        except KeyError:
            raise KeyError(
                "'cache_regions' argument is required on the "
                "Mako Lookup or Template object for usage "
                "with the dogpile.cache plugin.")

    def _get_region(self, **kw):
            region = kw['region']
        except KeyError:
            raise KeyError(
                "'cache_region' argument must be specified with 'cache=True'"
                "within templates for usage with the dogpile.cache plugin.")
            return self.regions[region]
        except KeyError:
            raise KeyError("No such region '%s'" % region)

    def get_and_replace(self, key, creation_function, **kw):
        return self._get_region(**kw).get_or_create(key, creation_function)

    def put(self, key, value, **kw):
        self._get_region(**kw).put(key, value)
    def get(self, key, **kw):
        return self._get_region(**kw).get(key)
    def invalidate(self, key, **kw):
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
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.