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