remove expired cache with config.cfgimpl_reset_cache(True)

This commit is contained in:
Emmanuel Garette 2013-04-19 22:42:33 +02:00
parent d7b5d9dc16
commit a11768ef43
3 changed files with 37 additions and 21 deletions

View file

@ -72,8 +72,8 @@ class SubConfig(object):
return return
self._setattr(name, value) self._setattr(name, value)
def cfgimpl_clean_cache(self): def cfgimpl_reset_cache(self):
self.cfgimpl_get_context().cfgimpl_clean_cache() self.cfgimpl_get_context().cfgimpl_reset_cache()
def _setattr(self, name, value, force_permissive=False): def _setattr(self, name, value, force_permissive=False):
if '.' in name: if '.' in name:
@ -361,9 +361,9 @@ class Config(SubConfig):
def _cfgimpl_build_all_paths(self): def _cfgimpl_build_all_paths(self):
self._cfgimpl_descr.build_cache() self._cfgimpl_descr.build_cache()
def cfgimpl_clean_cache(self): def cfgimpl_reset_cache(self, only_expired=False):
self.cfgimpl_get_values().reset_cache() self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
self.cfgimpl_get_settings().reset_cache() self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
def unwrap_from_path(self, path): def unwrap_from_path(self, path):
"""convenience method to extract and Option() object from the Config() """convenience method to extract and Option() object from the Config()
@ -502,11 +502,11 @@ def mandatory_warnings(config):
:returns: generator of mandatory Option's path :returns: generator of mandatory Option's path
""" """
config.cfgimpl_clean_cache() config.cfgimpl_reset_cache()
for path in config.cfgimpl_get_description().getpaths(include_groups=True): for path in config.cfgimpl_get_description().getpaths(include_groups=True):
try: try:
config._getattr(path, force_properties=('mandatory',)) config._getattr(path, force_properties=('mandatory',))
except PropertiesOptionError, err: except PropertiesOptionError, err:
if err.proptype == ['mandatory']: if err.proptype == ['mandatory']:
yield path yield path
config.cfgimpl_clean_cache() config.cfgimpl_reset_cache()

View file

@ -181,7 +181,7 @@ class Setting(object):
if propname not in props: if propname not in props:
props.append(propname) props.append(propname)
self.set_properties(props) self.set_properties(props)
self.context.cfgimpl_clean_cache() self.context.cfgimpl_reset_cache()
def disable_property(self, propname): def disable_property(self, propname):
"deletes property propname in the Config's properties attribute" "deletes property propname in the Config's properties attribute"
@ -189,7 +189,7 @@ class Setting(object):
if propname in props: if propname in props:
props.remove(propname) props.remove(propname)
self.set_properties(props) self.set_properties(props)
self.context.cfgimpl_clean_cache() self.context.cfgimpl_reset_cache()
def set_properties(self, properties, opt=None): def set_properties(self, properties, opt=None):
"""save properties for specified opt """save properties for specified opt
@ -209,14 +209,14 @@ class Setting(object):
if not propname in properties: if not propname in properties:
properties.append(propname) properties.append(propname)
self.set_properties(properties, opt) self.set_properties(properties, opt)
self.context.cfgimpl_clean_cache() self.context.cfgimpl_reset_cache()
def del_property(self, propname, opt, is_apply_req=True): def del_property(self, propname, opt, is_apply_req=True):
properties = self.get_properties(opt, is_apply_req) properties = self.get_properties(opt, is_apply_req)
if propname in properties: if propname in properties:
properties.remove(propname) properties.remove(propname)
self.set_properties(properties, opt) self.set_properties(properties, opt)
self.context.cfgimpl_clean_cache() self.context.cfgimpl_reset_cache()
def _validate_mandatory(self, opt, value, force_properties=None): def _validate_mandatory(self, opt, value, force_properties=None):
set_mandatory = self.has_property('mandatory') set_mandatory = self.has_property('mandatory')
@ -248,9 +248,9 @@ class Setting(object):
force_properties=None): force_properties=None):
is_cached = False is_cached = False
if opt_or_descr in self._cache: if opt_or_descr in self._cache:
t = time() exp = time()
props, raise_text, created = self._cache[opt_or_descr] props, raise_text, created = self._cache[opt_or_descr]
if t < created: if exp < created:
properties = props properties = props
is_cached = True is_cached = True
if not is_cached: if not is_cached:
@ -320,8 +320,16 @@ class Setting(object):
if self.has_property('expire'): if self.has_property('expire'):
self._cache[opt] = (props, raise_text, time() + expires_time) self._cache[opt] = (props, raise_text, time() + expires_time)
def reset_cache(self): def reset_cache(self, only_expired):
self._cache = {} if only_expired:
exp = time()
keys = self._cache.keys()
for key in keys:
props, raise_text, created = self._cache[key]
if exp > created:
del(self._cache[key])
else:
self._cache.clear()
def apply_requires(opt, config): def apply_requires(opt, config):

View file

@ -56,7 +56,7 @@ class Values(object):
def _reset(self, opt): def _reset(self, opt):
if opt in self._values: if opt in self._values:
self.context.cfgimpl_clean_cache() self.context.cfgimpl_reset_cache()
del(self._values[opt]) del(self._values[opt])
def _is_empty(self, opt, value): def _is_empty(self, opt, value):
@ -84,9 +84,9 @@ class Values(object):
def getitem(self, opt, validate=True, force_permissive=False, def getitem(self, opt, validate=True, force_permissive=False,
force_properties=None): force_properties=None):
if opt in self._cache: if opt in self._cache:
t = time() exp = time()
value, created = self._cache[opt] value, created = self._cache[opt]
if t < created: if exp < created:
return value return value
val = self._getitem(opt, validate, force_permissive, force_properties) val = self._getitem(opt, validate, force_permissive, force_properties)
self._set_cache(opt, val) self._set_cache(opt, val)
@ -145,7 +145,7 @@ class Values(object):
force_properties=force_properties) force_properties=force_properties)
def _setvalue(self, opt, value, force_permissive=False, force_properties=None): def _setvalue(self, opt, value, force_permissive=False, force_properties=None):
self.context.cfgimpl_clean_cache() self.context.cfgimpl_reset_cache()
setting = self.context.cfgimpl_get_settings() setting = self.context.cfgimpl_get_settings()
setting.validate_properties(opt, False, True, setting.validate_properties(opt, False, True,
value=value, value=value,
@ -175,8 +175,16 @@ class Values(object):
if self.context.cfgimpl_get_settings().has_property('expire'): if self.context.cfgimpl_get_settings().has_property('expire'):
self._cache[opt] = (val, time() + expires_time) self._cache[opt] = (val, time() + expires_time)
def reset_cache(self): def reset_cache(self, only_expired):
self._cache = {} if only_expired:
exp = time()
keys = self._cache.keys()
for key in keys:
val, created = self._cache[key]
if exp > created:
del(self._cache[key])
else:
self._cache.clear()
def __contains__(self, opt): def __contains__(self, opt):
return opt in self._values return opt in self._values