Skip to content
Myr

cache_extension.py

This file

Path

docs/examples/cache_extension.py

54 lines · 2.0 KiB · Python

Last changed by

Not known.

History of this file

Take it

1from jinja2 import nodes2from jinja2.ext import Extension345class FragmentCacheExtension(Extension):6    # a set of names that trigger the extension.7    tags = {"cache"}89    def __init__(self, environment):10        super().__init__(environment)1112        # add the defaults to the environment13        environment.extend(fragment_cache_prefix="", fragment_cache=None)1415    def parse(self, parser):16        # the first token is the token that started the tag.  In our case17        # we only listen to ``'cache'`` so this will be a name token with18        # `cache` as value.  We get the line number so that we can give19        # that line number to the nodes we create by hand.20        lineno = next(parser.stream).lineno2122        # now we parse a single expression that is used as cache key.23        args = [parser.parse_expression()]2425        # if there is a comma, the user provided a timeout.  If not use26        # None as second parameter.27        if parser.stream.skip_if("comma"):28            args.append(parser.parse_expression())29        else:30            args.append(nodes.Const(None))3132        # now we parse the body of the cache block up to `endcache` and33        # drop the needle (which would always be `endcache` in that case)34        body = parser.parse_statements(["name:endcache"], drop_needle=True)3536        # now return a `CallBlock` node that calls our _cache_support37        # helper method on this extension.38        return nodes.CallBlock(39            self.call_method("_cache_support", args), [], [], body40        ).set_lineno(lineno)4142    def _cache_support(self, name, timeout, caller):43        """Helper callback."""44        key = self.environment.fragment_cache_prefix + name4546        # try to load the block from the cache47        # if there is no fragment in the cache, render it and store48        # it in the cache.49        rv = self.environment.fragment_cache.get(key)50        if rv is not None:51            return rv52        rv = caller()53        self.environment.fragment_cache.add(key, rv, timeout)54        return rv