add 'cache' property

This commit is contained in:
2013-09-07 17:25:22 +02:00
parent f8b0a53c3f
commit 77c1ccf40b
6 changed files with 187 additions and 121 deletions

View File

@ -35,7 +35,7 @@ ro_append = set(['frozen', 'disabled', 'validator', 'everything_frozen',
'mandatory'])
rw_remove = set(['permissive', 'everything_frozen', 'mandatory'])
rw_append = set(['frozen', 'disabled', 'validator', 'hidden'])
default_properties = ('expire', 'validator')
default_properties = ('cache', 'expire', 'validator')
class _NameSpace:
@ -242,18 +242,21 @@ class Settings(object):
raise ValueError(_('if opt is not None, path should not be'
' None in _getproperties'))
ntime = None
if self._p_.hascache('property', path):
ntime = time()
is_cached, props = self._p_.getcache('property', path, ntime)
if 'cache' in self and self._p_.hascache(path):
if 'expire' in self:
ntime = int(time())
is_cached, props = self._p_.getcache(path, ntime)
if is_cached:
return props
props = self._p_.getproperties(path, opt._properties)
if is_apply_req:
props |= self.apply_requires(opt, path)
if 'expire' in self:
if ntime is None:
ntime = time()
self._p_.setcache('property', path, props, ntime + expires_time)
if 'cache' in self:
if 'expire' in self:
if ntime is None:
ntime = int(time())
ntime = ntime + expires_time
self._p_.setcache(path, props, ntime)
return props
def append(self, propname):
@ -387,9 +390,9 @@ class Settings(object):
def reset_cache(self, only_expired):
if only_expired:
self._p_.reset_expired_cache('property', time())
self._p_.reset_expired_cache(int(time()))
else:
self._p_.reset_all_cache('property')
self._p_.reset_all_cache()
def apply_requires(self, opt, path):
"""carries out the jit (just in time) requirements between options

View File

@ -27,34 +27,33 @@ class Cache(object):
self._cache = {}
self.storage = storage
def setcache(self, cache_type, path, val, time):
def setcache(self, path, val, time):
self._cache[path] = (val, time)
def getcache(self, cache_type, path, exp):
def getcache(self, path, exp):
value, created = self._cache[path]
if exp < created:
if exp <= created:
return True, value
return False, None
def hascache(self, cache_type, path):
def hascache(self, path):
""" path is in the cache
:param cache_type: value | property
:param path: the path's option
"""
return path in self._cache
def reset_expired_cache(self, cache_type, exp):
def reset_expired_cache(self, exp):
for key in tuple(self._cache.keys()):
val, created = self._cache[key]
if exp > created:
if created is not None and exp > created:
del(self._cache[key])
def reset_all_cache(self, cache_type):
def reset_all_cache(self):
"empty the cache"
self._cache.clear()
def get_cached(self, cache_type, context):
def get_cached(self, context):
"""return all values in a dictionary
example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')}
"""

View File

@ -147,25 +147,28 @@ class Values(object):
def getitem(self, opt, path=None, validate=True, force_permissive=False,
force_properties=None, validate_properties=True):
ntime = None
if path is None:
path = self._get_opt_path(opt)
if self._p_.hascache('value', path):
ntime = time()
is_cached, value = self._p_.getcache('value', path, ntime)
ntime = None
setting = self.context().cfgimpl_get_settings()
if 'cache' in setting and self._p_.hascache(path):
if 'expire' in setting:
ntime = int(time())
is_cached, value = self._p_.getcache(path, ntime)
if is_cached:
if opt.impl_is_multi() and not isinstance(value, Multi):
#load value so don't need to validate if is not a Multi
value = Multi(value, self.context, opt, path, validate=False)
return value
val = self._getitem(opt, path, validate, force_permissive, force_properties,
validate_properties)
if 'expire' in self.context().cfgimpl_get_settings() and validate and \
validate_properties and force_permissive is False and \
force_properties is None:
if ntime is None:
ntime = time()
self._p_.setcache('value', path, val, ntime + expires_time)
val = self._getitem(opt, path, validate, force_permissive,
force_properties, validate_properties)
if 'cache' in setting and validate and validate_properties and \
force_permissive is False and force_properties is None:
if 'expire' in setting:
if ntime is None:
ntime = int(time())
ntime = ntime + expires_time
self._p_.setcache(path, val, ntime)
return val
@ -300,9 +303,9 @@ class Values(object):
clears the cache if necessary
"""
if only_expired:
self._p_.reset_expired_cache('value', time())
self._p_.reset_expired_cache(int(time()))
else:
self._p_.reset_all_cache('value')
self._p_.reset_all_cache()
def _get_opt_path(self, opt):
"""