reset cache only if calculated value return a new value
This commit is contained in:
@ -31,7 +31,7 @@ STATIC_TUPLE = frozenset()
|
||||
|
||||
|
||||
submulti = 2
|
||||
NAME_REGEXP = re.compile(r'^[a-z1-Z][a-zA-Z\d_-]*$')
|
||||
NAME_REGEXP = re.compile(r'^[a-zA-Z][a-zA-Z\d_-]*$')
|
||||
FORBIDDEN_NAMES = frozenset(['iter_all', 'iter_group', 'find', 'find_first',
|
||||
'make_dict', 'unwrap_from_path', 'read_only',
|
||||
'read_write', 'getowner', 'set_contexts'])
|
||||
|
@ -15,7 +15,7 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
# ____________________________________________________________
|
||||
def POUET(obj):
|
||||
def _display_classname(obj):
|
||||
return(obj.__class__.__name__.lower())
|
||||
|
||||
DEBUG = False
|
||||
@ -34,23 +34,25 @@ class Cache(object):
|
||||
"""add val in cache for a specified path
|
||||
if slave, add index
|
||||
"""
|
||||
if DEBUG and path == 'odmaster.third':
|
||||
print('ca set cache', path, val, POUET(self), id(self))
|
||||
if DEBUG:
|
||||
print('setcache', path, val, _display_classname(self), id(self))
|
||||
self._cache.setdefault(path, {})[index] = (val, time)
|
||||
|
||||
def getcache(self, path, exp, index):
|
||||
value, created = self._cache[path][index]
|
||||
if created is None or exp <= created:
|
||||
if DEBUG and path == 'odmaster.third':
|
||||
print('ca trouve dans le cache', path, value, POUET(self), id(self), index, exp)
|
||||
if created is None or exp is None or exp <= created:
|
||||
if DEBUG:
|
||||
print('getcache in cache', path, value, _display_classname(self), id(self), index, exp)
|
||||
return True, value
|
||||
if DEBUG:
|
||||
print('getcache not in cache')
|
||||
return False, None # pragma: no cover
|
||||
|
||||
def delcache(self, path):
|
||||
"""remove cache for a specified path
|
||||
"""
|
||||
if DEBUG and path == 'odmaster.third':
|
||||
print('ca del cache', path, POUET(self), id(self))
|
||||
if DEBUG:
|
||||
print('delcache', path, _display_classname(self), id(self))
|
||||
if path in self._cache:
|
||||
del self._cache[path]
|
||||
|
||||
@ -59,8 +61,8 @@ class Cache(object):
|
||||
|
||||
:param path: the path's option
|
||||
"""
|
||||
if DEBUG and path == 'odmaster.third':
|
||||
print('ca cherche dans le cache', path, POUET(self), id(self))
|
||||
if DEBUG:
|
||||
print('hascache', path, _display_classname(self), id(self))
|
||||
return path in self._cache and index in self._cache[path]
|
||||
|
||||
def reset_expired_cache(self, exp):
|
||||
@ -77,7 +79,7 @@ class Cache(object):
|
||||
def reset_all_cache(self):
|
||||
"empty the cache"
|
||||
if DEBUG:
|
||||
print('bzzzzzzzzzzzz delete tout le cache', POUET(self), id(self))
|
||||
print('reset_all_cache', _display_classname(self), id(self))
|
||||
self._cache.clear()
|
||||
|
||||
def get_cached(self):
|
||||
|
@ -88,7 +88,7 @@ class Values(object):
|
||||
if setting_properties and 'cache' in setting_properties and \
|
||||
self._p_.hascache(path,
|
||||
index):
|
||||
if 'expire' in setting_properties:
|
||||
if 'expire' in setting_properties or 'expire' in config_bag.properties:
|
||||
ntime = int(time())
|
||||
is_cached, value = self._p_.getcache(path,
|
||||
ntime,
|
||||
@ -118,7 +118,7 @@ class Values(object):
|
||||
# store value in cache
|
||||
if not is_cached and \
|
||||
setting_properties and 'cache' in setting_properties:
|
||||
if 'expire' in setting_properties:
|
||||
if 'expire' in setting_properties or 'expire' in config_bag.properties:
|
||||
if ntime is None:
|
||||
ntime = int(time())
|
||||
ntime = ntime + expires_time
|
||||
@ -198,8 +198,12 @@ class Values(object):
|
||||
config_bag):
|
||||
context = self._getcontext()
|
||||
opt = config_bag.option
|
||||
def _reset_cache():
|
||||
# calculated value could be a new value, so reset cache
|
||||
def _reset_cache(_value):
|
||||
if self._p_.hascache(path, index):
|
||||
is_cache, cache_value = self._p_.getcache(path, None, index)
|
||||
if is_cache and cache_value == _value:
|
||||
return
|
||||
# calculated value is a new value, so reset cache
|
||||
context.cfgimpl_reset_cache(opt=opt,
|
||||
path=path)
|
||||
|
||||
@ -236,41 +240,40 @@ class Values(object):
|
||||
# if value is a list and index is set
|
||||
if opt.impl_is_submulti() and (value == [] or not isinstance(value[0], list)):
|
||||
# return value only if it's a submulti and not a list of list
|
||||
_reset_cache()
|
||||
_reset_cache(value,)
|
||||
return value
|
||||
|
||||
if len(value) > index:
|
||||
# return the value for specified index if found
|
||||
_reset_cache()
|
||||
_reset_cache(value[index])
|
||||
return value[index]
|
||||
# there is no calculate value for this index,
|
||||
# so return an other default value
|
||||
elif isinstance(value, list):
|
||||
# value is a list, but no index specified
|
||||
_reset_cache()
|
||||
if opt.impl_is_submulti() and (value != [] and not isinstance(value[0], list)):
|
||||
# if submulti, return a list of value
|
||||
return [value]
|
||||
value = [value]
|
||||
_reset_cache(value)
|
||||
return value
|
||||
# otherwise just return the value
|
||||
return value
|
||||
elif index is not None:
|
||||
# if not list but with index
|
||||
_reset_cache()
|
||||
if opt.impl_is_submulti():
|
||||
# if submulti, return a list of value
|
||||
return [value]
|
||||
# otherwise just return the value
|
||||
value = [value]
|
||||
_reset_cache(value)
|
||||
return value
|
||||
else:
|
||||
_reset_cache()
|
||||
# not a list or index is None
|
||||
if opt.impl_is_submulti():
|
||||
# return a list of list for a submulti
|
||||
return [[value]]
|
||||
value = [[value]]
|
||||
elif opt.impl_is_multi():
|
||||
# return a list for a multi
|
||||
return [value]
|
||||
# not a list, return value
|
||||
value = [value]
|
||||
_reset_cache(value)
|
||||
return value
|
||||
|
||||
# now try to get default value:
|
||||
|
Reference in New Issue
Block a user