save indexed value in cache is not necessary

This commit is contained in:
Emmanuel Garette 2017-09-22 19:51:26 +02:00
parent d6bbbd87f5
commit e32820c913
3 changed files with 67 additions and 13 deletions

View File

@ -7,7 +7,7 @@ setting.expires_time = 1
value.expires_time = 1 value.expires_time = 1
from tiramisu.option import BoolOption, IPOption, IntOption, StrOption, OptionDescription from tiramisu.option import BoolOption, IPOption, IntOption, StrOption, OptionDescription
from tiramisu.config import Config from tiramisu.config import Config
from tiramisu.error import ConfigError from tiramisu.error import ConfigError, PropertiesOptionError
from tiramisu.setting import groups 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.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 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 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'][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() 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.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]) 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.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 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 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'][None][0] == [None, 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
assert cache['ip_admin_eth0.netmask_admin_eth0'][1][0] is None #assert cache['ip_admin_eth0.netmask_admin_eth0'][1][0] is None
cache = cfg.cfgimpl_get_settings()._p_.get_cached() 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.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]) 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.val1': {None: (set(['empty']), None)},
'val1.val2': {None: (set([]), None), 0: (set([]), None)}} 'val1.val2': {None: (set([]), None), 0: (set([]), None)}}
assert cfg.cfgimpl_get_values()._p_.get_cached() == {'val1.val1': {None: ([None], 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.val1.val1.append()
cfg.cfgimpl_get_values().force_cache() cfg.cfgimpl_get_values().force_cache()
cfg.val1.val2[1] = 'oui' cfg.val1.val2[1] = 'oui'
@ -546,7 +546,7 @@ def test_cache_master_and_slaves_master():
'val1.val1': {None: (set(['empty']), None)}, 'val1.val1': {None: (set(['empty']), None)},
'val1.val2': {None: (set([]), None), 0: (set([]), None), 1: (set([]), 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)}, 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(): 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)}, #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), 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(): def test_cache_requires():
a = BoolOption('activate_service', '', True) a = BoolOption('activate_service', '', True)

View File

@ -382,7 +382,7 @@ class Settings(object):
if opt is None: if opt is None:
ntime = int(time()) ntime = int(time())
if self._p_.hascache(path, index): 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: else:
is_cached = False is_cached = False
if not is_cached or 'cache' not in props: if not is_cached or 'cache' not in props:
@ -396,7 +396,7 @@ class Settings(object):
ntime = ntime + expires_time ntime = ntime + expires_time
else: else:
ntime = None ntime = None
self._p_.setcache(path, props, ntime, index) self._p_.setcache(path, props, ntime, None)
else: else:
if path is None: # pragma: optional cover if path is None: # pragma: optional cover
raise ValueError(_('if opt is not None, path should not be' raise ValueError(_('if opt is not None, path should not be'

View File

@ -245,7 +245,9 @@ class Values(object):
_orig_context is undefined: _orig_context is undefined:
if 'expire' in setting_properties: if 'expire' in setting_properties:
ntime = int(time()) 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 is_cached:
if opt.impl_is_multi() and not isinstance(value, Multi) and index is None: if opt.impl_is_multi() and not isinstance(value, Multi) and index is None:
value = Multi(value, self.context, opt, path) value = Multi(value, self.context, opt, path)
@ -290,14 +292,14 @@ class Values(object):
if isinstance(val, Exception): if isinstance(val, Exception):
return val return val
# cache doesn't work with SubMulti yet # 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 \ validate and validate_properties and force_permissive is False \
and trusted_cached_properties is True and _orig_context is undefined: and trusted_cached_properties is True and _orig_context is undefined:
if 'expire' in setting_properties: if 'expire' in setting_properties:
if ntime is None: if ntime is None:
ntime = int(time()) ntime = int(time())
ntime = ntime + expires_time ntime = ntime + expires_time
self._p_.setcache(path, val, ntime, index) self._p_.setcache(path, val, ntime, None)
return val return val
def _get_validated_value(self, opt, path, validate, force_permissive, def _get_validated_value(self, opt, path, validate, force_permissive,