Last active
January 15, 2022 21:23
-
-
Save Shu-Ji/b63df32abf3b77dd1eaa to your computer and use it in GitHub Desktop.
jinja2 redis cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jinja2 import nodes | |
from jinja2.ext import Extension | |
class FragmentCacheExtension(Extension): | |
'''http://jinja.pocoo.org/docs/dev/extensions/#example-extension''' | |
# 其余部分略... | |
def _cache_support(self, name, timeout, caller): | |
if timeout <= 0: # 0 or -1 DO NOT do cache. (useful in debug mode) | |
return caller() | |
key = self.environment.fragment_cache_prefix + name | |
rv = self.environment.fragment_cache.get(key) | |
if rv is not None: | |
return rv | |
rv = caller() | |
self.environment.fragment_cache.add(key, rv, timeout) | |
return rv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment