Skip to content

Commit

Permalink
[sonic-cfggen] store jinja2 cache in log level db. (#5646)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
stepanblyschak authored Oct 16, 2020
1 parent d8363a9 commit 8df0e2b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/sonic-config-engine/redis_bcc.py
Original file line number Diff line number Diff line change
@@ -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 """

Expand All @@ -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()))

0 comments on commit 8df0e2b

Please sign in to comment.