-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjinja_cache.py
25 lines (22 loc) · 899 Bytes
/
jinja_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import cache
import jinja2
class KeystoreCache(jinja2.BytecodeCache):
def __init__(self):
super().__init__()
self._cache_connection = None
def load_bytecode(self, bucket):
cache_key = self._template_key(bucket.key)
self._ensure_connection()
cached_bytecode = self._cache_connection.get(cache_key)
if cached_bytecode:
bucket.bytecode_from_string(bytes(cached_bytecode, "utf-8"))
def dump_bytecode(self, bucket):
cache_key = self._template_key(bucket.key)
bytecode = str(bucket.bytecode_to_string())
self._ensure_connection()
self._cache_connection.set(cache_key, bytecode)
def _ensure_connection(self):
if not self._cache_connection:
self._cache_connection = cache.Cache()
def _template_key(self, template_name):
return "jinja2-template-%s" % template_name