| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 1 | from jinja2 import nodes |
| 2 | from jinja2.ext import Extension |
| 3 | |
| 4 | |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 5 | class FragmentCacheExtension(Extension): |
| 6 | # a set of names that trigger the extension. |
| David Lord | 04c8787 | 2020-01-10 07:46:18 -0800 | [diff] [blame] | 7 | tags = {"cache"} |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 8 | |
| 9 | def __init__(self, environment): |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 10 | super(FragmentCacheExtension, self).__init__(environment) |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 11 | |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 12 | # add the defaults to the environment |
| David Lord | 04c8787 | 2020-01-10 07:46:18 -0800 | [diff] [blame] | 13 | environment.extend(fragment_cache_prefix="", fragment_cache=None) |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 14 | |
| 15 | def parse(self, parser): |
| 16 | # the first token is the token that started the tag. In our case |
| 17 | # we only listen to ``'cache'`` so this will be a name token with |
| 18 | # `cache` as value. We get the line number so that we can give |
| 19 | # that line number to the nodes we create by hand. |
| jenisys | fa3a302 | 2015-05-09 18:57:52 +0200 | [diff] [blame] | 20 | lineno = next(parser.stream).lineno |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 21 | |
| 22 | # now we parse a single expression that is used as cache key. |
| 23 | args = [parser.parse_expression()] |
| 24 | |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 25 | # if there is a comma, the user provided a timeout. If not use |
| 26 | # None as second parameter. |
| David Lord | 04c8787 | 2020-01-10 07:46:18 -0800 | [diff] [blame] | 27 | if parser.stream.skip_if("comma"): |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 28 | args.append(parser.parse_expression()) |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 29 | else: |
| 30 | args.append(nodes.Const(None)) |
| 31 | |
| 32 | # now we parse the body of the cache block up to `endcache` and |
| 33 | # drop the needle (which would always be `endcache` in that case) |
| David Lord | 04c8787 | 2020-01-10 07:46:18 -0800 | [diff] [blame] | 34 | body = parser.parse_statements(["name:endcache"], drop_needle=True) |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 35 | |
| 36 | # now return a `CallBlock` node that calls our _cache_support |
| 37 | # helper method on this extension. |
| David Lord | 04c8787 | 2020-01-10 07:46:18 -0800 | [diff] [blame] | 38 | return nodes.CallBlock( |
| 39 | self.call_method("_cache_support", args), [], [], body |
| 40 | ).set_lineno(lineno) |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 41 | |
| 42 | def _cache_support(self, name, timeout, caller): |
| 43 | """Helper callback.""" |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 44 | key = self.environment.fragment_cache_prefix + name |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 45 | |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 46 | # try to load the block from the cache |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 47 | # if there is no fragment in the cache, render it and store |
| 48 | # it in the cache. |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 49 | rv = self.environment.fragment_cache.get(key) |
| Armin Ronacher | 7850dc5 | 2009-02-08 11:11:57 +0100 | [diff] [blame] | 50 | if rv is not None: |
| Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 51 | return rv |
| Armin Ronacher | d89f0f3 | 2009-02-04 18:57:27 +0100 | [diff] [blame] | 52 | rv = caller() |
| 53 | self.environment.fragment_cache.add(key, rv, timeout) |
| Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 54 | return rv |