Skip to content

Instantly share code, notes, and snippets.

@Shu-Ji
Last active January 15, 2022 21:23
Show Gist options
  • Save Shu-Ji/b63df32abf3b77dd1eaa to your computer and use it in GitHub Desktop.
Save Shu-Ji/b63df32abf3b77dd1eaa to your computer and use it in GitHub Desktop.
jinja2 redis cache
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