summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2020-07-17 12:08:11 -0500
committerJohn Estabrook <jestabro@vyos.io>2020-07-17 12:08:11 -0500
commita64910c3c0e22984e20f346c79db5e51c52ef951 (patch)
tree6faec5176af6047550b263d4a1fae8ff377dcd2a /python
parent7291da42df3b52b36d748d8f6c86e5530b998ff6 (diff)
downloadvyos-1x-a64910c3c0e22984e20f346c79db5e51c52ef951.tar.gz
vyos-1x-a64910c3c0e22984e20f346c79db5e51c52ef951.zip
config: T2689: cache config_dict
This is a minor modification of the implementation by Thomas Mangin.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/config.py32
1 files changed, 24 insertions, 8 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py
index 780b48a7b..5d58316e7 100644
--- a/python/vyos/config.py
+++ b/python/vyos/config.py
@@ -67,6 +67,7 @@ import os
import re
import json
import subprocess
+from copy import deepcopy
import vyos.util
import vyos.configtree
@@ -91,6 +92,8 @@ class Config(object):
def __init__(self, session_env=None):
self._cli_shell_api = "/bin/cli-shell-api"
self._level = []
+ self._dict_cache = {}
+
if session_env:
self.__session_env = session_env
else:
@@ -287,6 +290,24 @@ class Config(object):
self.__session_env = save_env
return(default)
+ def get_cached_dict(self, effective=False):
+ cached = self._dict_cache.get(effective, {})
+ if cached:
+ config_dict = cached
+ else:
+ config_dict = {}
+
+ if effective:
+ if self._running_config:
+ config_dict = json.loads((self._running_config).to_json())
+ else:
+ if self._session_config:
+ config_dict = json.loads((self._session_config).to_json())
+
+ self._dict_cache[effective] = config_dict
+
+ return config_dict
+
def get_config_dict(self, path=[], effective=False, key_mangling=None, get_first_key=False):
"""
Args:
@@ -297,14 +318,7 @@ class Config(object):
Returns: a dict representation of the config under path
"""
- config_dict = {}
-
- if effective:
- if self._running_config:
- config_dict = json.loads((self._running_config).to_json())
- else:
- if self._session_config:
- config_dict = json.loads((self._session_config).to_json())
+ config_dict = self.get_cached_dict(effective)
config_dict = vyos.util.get_sub_dict(config_dict, self._make_path(path), get_first_key)
@@ -316,6 +330,8 @@ class Config(object):
raise ValueError("key_mangling must be a tuple of two strings")
else:
config_dict = vyos.util.mangle_dict_keys(config_dict, key_mangling[0], key_mangling[1])
+ else:
+ config_dict = deepcopy(config_dict)
return config_dict