tiramisu/tiramisu/storage/util.py

113 lines
5.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2014-01-20 14:53:08 +01:00
"utils used by storage"
2019-02-12 06:55:47 +01:00
# Copyright (C) 2013-2019 Team tiramisu (see AUTHORS for all contributors)
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
2013-09-22 22:33:09 +02:00
# 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/>.
# ____________________________________________________________
2018-06-25 21:40:16 +02:00
from time import time
2018-09-07 08:42:14 +02:00
from .cache.dictionary import Cache as DictCache
2019-02-24 19:03:00 +01:00
from ..log import log
2018-06-25 21:40:16 +02:00
2019-02-23 22:52:06 +01:00
def _display_classname(obj): # pragma: no cover
2017-11-12 14:33:05 +01:00
return(obj.__class__.__name__.lower())
2013-09-22 20:57:52 +02:00
2018-06-25 21:40:16 +02:00
class Cache(DictCache):
__slots__ = ('_storage',)
def __init__(self, storage):
2013-09-22 20:57:52 +02:00
self._storage = storage
2018-06-25 21:40:16 +02:00
super().__init__()
2013-09-22 20:57:52 +02:00
2018-08-18 08:35:30 +02:00
def setcache(self, path, index, val, self_props, props):
2017-07-08 15:59:56 +02:00
"""add val in cache for a specified path
2019-02-23 19:06:23 +01:00
if follower, add index
2017-07-08 15:59:56 +02:00
"""
2018-08-18 08:35:30 +02:00
if 'cache' in props or 'cache' in self_props:
2019-02-24 19:03:00 +01:00
log.debug('setcache {} with index {} and value {} in {} ({})'.format(path, index, val,
2018-06-25 21:40:16 +02:00
_display_classname(self),
id(self)))
self._setcache(path, index, val, time())
2019-02-24 19:03:00 +01:00
log.debug('not setcache {} with index {} and value {} and props {} and {} in {} ({})'.format(path,
2018-08-01 08:37:58 +02:00
index,
val,
props,
self_props,
_display_classname(self),
id(self)))
2018-06-25 21:40:16 +02:00
def getcache(self,
path,
2019-02-24 10:36:42 +01:00
expiration_time,
2018-06-25 21:40:16 +02:00
index,
props,
self_props,
type_):
no_cache = False, None
2018-09-12 21:05:14 +02:00
if 'cache' in props or type_ == 'context_props':
2018-06-25 21:40:16 +02:00
indexed = self._getcache(path, index)
if indexed is None:
return no_cache
value, timestamp = indexed
if type_ == 'context_props':
# cached value is settings properties so value is props
props = value
elif type_ == 'self_props':
# if self_props is None, so cached value is self properties
# so value is self_props
self_props = value
# recheck "cache" value
2018-08-18 08:35:30 +02:00
if 'cache' in props or 'cache' in props:
2019-02-24 10:36:42 +01:00
if expiration_time and timestamp and \
2018-08-18 08:35:30 +02:00
('expire' in props or \
'expire' in self_props):
2018-06-25 21:40:16 +02:00
ntime = int(time())
2019-02-24 10:36:42 +01:00
if timestamp + expiration_time >= ntime:
2019-02-24 19:03:00 +01:00
log.debug('getcache in cache (1)', path, value, _display_classname(self),
2018-06-25 21:40:16 +02:00
id(self), index)
return True, value
else:
2019-02-24 19:03:00 +01:00
log.debug('getcache expired value for path {} < {}'.format(
timestamp + expiration_time, ntime))
2018-06-25 21:40:16 +02:00
# if expired, remove from cache
2018-09-04 08:36:02 +02:00
#self.delcache(path)
2018-06-25 21:40:16 +02:00
else:
2019-02-24 19:03:00 +01:00
log.debug('getcache in cache (2)', path, value, _display_classname(self),
2018-06-25 21:40:16 +02:00
id(self), index)
return True, value
2019-02-24 19:03:00 +01:00
log.debug('getcache {} with index {} not in {} cache'.format(path, index,
2018-06-25 21:40:16 +02:00
_display_classname(self)))
return no_cache
2017-07-08 15:59:56 +02:00
def delcache(self, path):
"""remove cache for a specified path
"""
2019-02-24 19:03:00 +01:00
log.debug('delcache', path, _display_classname(self), id(self))
2017-07-08 15:59:56 +02:00
if path in self._cache:
2018-06-25 21:40:16 +02:00
self._delcache(path)
2013-09-07 17:25:22 +02:00
def reset_all_cache(self):
"empty the cache"
2019-02-24 19:03:00 +01:00
log.debug('reset_all_cache', _display_classname(self), id(self))
2018-06-25 21:40:16 +02:00
self._reset_all_cache()
2017-09-22 08:26:11 +02:00
def get_cached(self):
"""return all values in a dictionary
2018-06-25 21:40:16 +02:00
please only use it in test purpose
example: {'path1': {'index1': ('value1', 'time1')}, 'path2': {'index2': ('value2', 'time2', )}}
"""
2019-02-24 19:03:00 +01:00
log.debug('get_chached {} for {} ({})'.format(self._cache, _display_classname(self), id(self)))
2018-06-25 21:40:16 +02:00
return self._get_cached()