[sonic-cfggen] store jinja2 cache in log level db. (#5646)

This PR makes two changes:
    - Store Jinja2 cache in LOGLEVEL DB instead of STATE DB
    - Store bytecode cache encoded in base64

Tested with the following command: "redis-dump -d 3 -k JINJA2_CACHE"

Signed-off-by: Stepan Blyschak <stepanb@nvidia.com>
This commit is contained in:
Stepan Blyshchak 2020-10-16 22:00:57 +03:00 committed by Abhishek Dosi
parent 73647be598
commit fd153e0584

View File

@ -1,5 +1,7 @@
import jinja2
from base64 import b64encode, b64decode
class RedisBytecodeCache(jinja2.BytecodeCache):
""" A bytecode cache for jinja2 template that stores bytecode in Redis """
@ -8,19 +10,20 @@ class RedisBytecodeCache(jinja2.BytecodeCache):
def __init__(self, client):
self._client = client
try:
self._client.connect(self._client.STATE_DB, retry_on=False)
self._client.connect(self._client.LOGLEVEL_DB, retry_on=False)
except Exception:
self._client = None
def load_bytecode(self, bucket):
if self._client is None:
return
code = self._client.get(self._client.STATE_DB, self.REDIS_HASH, bucket.key)
code = self._client.get(self._client.LOGLEVEL_DB, self.REDIS_HASH, bucket.key)
if code is not None:
bucket.bytecode_from_string(code)
bucket.bytecode_from_string(b64decode(code))
def dump_bytecode(self, bucket):
if self._client is None:
return
self._client.set(self._client.STATE_DB, self.REDIS_HASH, bucket.key, bucket.bytecode_to_string())
self._client.set(self._client.LOGLEVEL_DB, self.REDIS_HASH,
bucket.key, b64encode(bucket.bytecode_to_string()))