From e32820c913f96658d4e48bbaeeec16ffa3b5338f Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 22 Sep 2017 19:51:26 +0200 Subject: [PATCH] save indexed value in cache is not necessary --- test/test_cache.py | 68 +++++++++++++++++++++++++++++++++++++++------ tiramisu/setting.py | 4 +-- tiramisu/value.py | 8 ++++-- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index c311867..f150257 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -7,7 +7,7 @@ setting.expires_time = 1 value.expires_time = 1 from tiramisu.option import BoolOption, IPOption, IntOption, StrOption, OptionDescription from tiramisu.config import Config -from tiramisu.error import ConfigError +from tiramisu.error import ConfigError, PropertiesOptionError from tiramisu.setting import groups @@ -364,9 +364,9 @@ def test_cache_master_slave(): assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None]) assert cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2'] - assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None, 0]) + assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None]) assert cache['ip_admin_eth0.netmask_admin_eth0'][None][0] == [None] - assert cache['ip_admin_eth0.netmask_admin_eth0'][0][0] is None + #assert cache['ip_admin_eth0.netmask_admin_eth0'][0][0] is None cache = cfg.cfgimpl_get_settings()._p_.get_cached() assert set(cache.keys()) == set([None, 'ip_admin_eth0', 'ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache['ip_admin_eth0'].keys()) == set([None]) @@ -380,10 +380,10 @@ def test_cache_master_slave(): assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None]) assert cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2', '192.168.1.1'] - assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None, 0, 1]) + assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None]) assert cache['ip_admin_eth0.netmask_admin_eth0'][None][0] == [None, None] - assert cache['ip_admin_eth0.netmask_admin_eth0'][0][0] is None - assert cache['ip_admin_eth0.netmask_admin_eth0'][1][0] is None + #assert cache['ip_admin_eth0.netmask_admin_eth0'][0][0] is None + #assert cache['ip_admin_eth0.netmask_admin_eth0'][1][0] is None cache = cfg.cfgimpl_get_settings()._p_.get_cached() assert set(cache.keys()) == set([None, 'ip_admin_eth0', 'ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache['ip_admin_eth0'].keys()) == set([None]) @@ -533,7 +533,7 @@ def test_cache_master_and_slaves_master(): 'val1.val1': {None: (set(['empty']), None)}, 'val1.val2': {None: (set([]), None), 0: (set([]), None)}} assert cfg.cfgimpl_get_values()._p_.get_cached() == {'val1.val1': {None: ([None], None)}, - 'val1.val2': {None: ([None], None), 0: (None, None)}} + 'val1.val2': {None: ([None], None)}} cfg.val1.val1.append() cfg.cfgimpl_get_values().force_cache() cfg.val1.val2[1] = 'oui' @@ -546,7 +546,7 @@ def test_cache_master_and_slaves_master(): 'val1.val1': {None: (set(['empty']), None)}, 'val1.val2': {None: (set([]), None), 0: (set([]), None), 1: (set([]), None)}} assert cfg.cfgimpl_get_values()._p_.get_cached() == {'val1.val1': {None: ([None, None], None)}, - 'val1.val2': {None: ([None, 'oui'], None), 0: (None, None), 1: ('oui', None)}} + 'val1.val2': {None: ([None, 'oui'], None)}} def test_cache_master_callback(): @@ -576,6 +576,58 @@ def test_cache_master_callback(): #assert cfg.cfgimpl_get_values()._p_.get_cached() == {'val1.val1': {None: ([None], None)}, # 'val1.val2': {None: ([None], None), 0: (None, None)}} +def test_cache_master_slave_different(): + b = IntOption('int', 'Test int option', default=[0], multi=True) + c = StrOption('str', 'Test string option', multi=True) + d = StrOption('str1', 'Test string option', requires=[{'option': c, 'expected': None, 'action': 'hidden', 'inverse': True}], multi=True) + descr = OptionDescription("int", "", [b, c, d]) + descr.impl_set_group_type(groups.master) + cfg = Config(descr) + cfg.cfgimpl_get_settings().remove('expire') + cfg.read_write() + cfg.cfgimpl_get_values().force_cache() + assert cfg.cfgimpl_get_values()._p_.get_cached() == {'int': {None: ([0], None)}, + 'str': {None: ([None], None)}, + 'str1': {None: ([None], None)}} + assert cfg.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)}, + 'int': {None: (set(['empty']), None)}, + 'str': {None: (set([]), None), 0: (set([]), None)}, + 'str1': {None: (set([]), None), 0: (set([]), None)}} + cfg.int = [0, 1] + cfg.cfgimpl_get_values().force_cache() + assert cfg.cfgimpl_get_values()._p_.get_cached() == {'int': {None: ([0, 1], None)}, + 'str': {None: ([None, None], None)}, + 'str1': {None: ([None, None], None)}} + assert cfg.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)}, + 'int': {None: (set(['empty']), None)}, + 'str': {None: (set([]), None), 0: (set([]), None), 1: (set([]), None)}, + 'str1': {None: (set([]), None), 0: (set([]), None), 1: (set([]), None)}} + + cfg.str = [None, '1'] + cfg.cfgimpl_get_values().force_cache() + assert set(cfg.cfgimpl_get_values()._p_.get_cached().keys()) == set(['int', 'str', 'str1']) + assert cfg.cfgimpl_get_values()._p_.get_cached()['int'] == {None: ([0, 1], None)} + assert cfg.cfgimpl_get_values()._p_.get_cached()['str'] == {None: ([None, '1'], None)} + assert cfg.cfgimpl_get_values()._p_.get_cached()['str1'][None][0][0] == None + raises(PropertiesOptionError, "cfg.cfgimpl_get_values()._p_.get_cached()['str1'][None][0][1]") + assert cfg.cfgimpl_get_values()._p_.get_cached()['str1'][None][1] == None + assert cfg.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)}, + 'int': {None: (set(['empty']), None)}, + 'str': {None: (set([]), None), 0: (set([]), None), 1: (set([]), None)}, + 'str1': {None: (set([]), None), 0: (set([]), None), 1: (set(['hidden']), None)}} + cfg.read_only() + assert cfg.cfgimpl_get_values()._p_.get_cached() == {} + assert cfg.cfgimpl_get_settings()._p_.get_cached() == {} + cfg.cfgimpl_get_values().force_cache() + assert cfg.cfgimpl_get_values()._p_.get_cached() == {'int': {None: ([0, 1], None)}, + 'str': {None: ([None, '1'], None)}, + 'str1': {None: ([None, None], None)}} + assert cfg.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (set(['cache', 'disabled', 'frozen', 'everything_frozen', 'validator', 'warnings', 'empty', 'mandatory', ]), None)}, + 'int': {None: (set(['empty']), None)}, + 'str': {None: (set([]), None), 0: (set([]), None), 1: (set([]), None)}, + 'str1': {None: (set([]), None), 0: (set([]), None), 1: (set(['hidden']), None)}} + + def test_cache_requires(): a = BoolOption('activate_service', '', True) diff --git a/tiramisu/setting.py b/tiramisu/setting.py index d77ef5a..055e9e6 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -382,7 +382,7 @@ class Settings(object): if opt is None: ntime = int(time()) if self._p_.hascache(path, index): - is_cached, props = self._p_.getcache(path, ntime, index) + is_cached, props = self._p_.getcache(path, ntime, None) else: is_cached = False if not is_cached or 'cache' not in props: @@ -396,7 +396,7 @@ class Settings(object): ntime = ntime + expires_time else: ntime = None - self._p_.setcache(path, props, ntime, index) + self._p_.setcache(path, props, ntime, None) else: if path is None: # pragma: optional cover raise ValueError(_('if opt is not None, path should not be' diff --git a/tiramisu/value.py b/tiramisu/value.py index 75f010c..b716f1c 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -245,7 +245,9 @@ class Values(object): _orig_context is undefined: if 'expire' in setting_properties: ntime = int(time()) - is_cached, value = self._p_.getcache(path, ntime, index) + is_cached, value = self._p_.getcache(path, ntime, None) + if index: + value = value[index] if is_cached: if opt.impl_is_multi() and not isinstance(value, Multi) and index is None: value = Multi(value, self.context, opt, path) @@ -290,14 +292,14 @@ class Values(object): if isinstance(val, Exception): return val # cache doesn't work with SubMulti yet - if not isinstance(val, SubMulti) and 'cache' in setting_properties and \ + if not index and not isinstance(val, SubMulti) and 'cache' in setting_properties and \ validate and validate_properties and force_permissive is False \ and trusted_cached_properties is True and _orig_context is undefined: if 'expire' in setting_properties: if ntime is None: ntime = int(time()) ntime = ntime + expires_time - self._p_.setcache(path, val, ntime, index) + self._p_.setcache(path, val, ntime, None) return val def _get_validated_value(self, opt, path, validate, force_permissive,