tiramisu/tiramisu/cacheobj.py

104 lines
3.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-09-01 09:41:53 +02:00
"cache used by storage"
# Copyright (C) 2013-2026 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
2017-11-12 14:33:05 +01:00
2013-09-22 20:57:52 +02:00
class Cache:
2024-10-31 08:53:58 +01:00
"""cache object"""
__slots__ = ("_cache",)
def __init__(self):
self._cache = {}
2024-04-24 15:39:17 +02:00
def _get_path_index(self, subconfig):
if subconfig is None:
2023-05-11 15:44:48 +02:00
path = None
index = None
else:
2024-04-24 15:39:17 +02:00
path = subconfig.path
index = subconfig.index
2023-05-11 15:44:48 +02:00
return path, index
2024-10-31 08:53:58 +01:00
def getcache(
self,
subconfig,
type_,
expiration=True,
):
"""get the cache value fot a specified path"""
2023-05-11 15:44:48 +02:00
no_cache = False, None, False
2024-04-24 15:39:17 +02:00
path, index = self._get_path_index(subconfig)
2023-05-11 15:44:48 +02:00
if path not in self._cache or index not in self._cache[path]:
return no_cache
value, timestamp, validated = self._cache[path][index]
2024-06-20 12:56:27 +02:00
props = subconfig.config_bag.properties
2024-10-31 08:53:58 +01:00
if type_ == "self_props":
2024-06-20 12:56:27 +02:00
# cached value is self_props
self_props = value
2023-05-11 15:44:48 +02:00
else:
2024-06-20 12:56:27 +02:00
self_props = subconfig.properties
2024-10-31 08:53:58 +01:00
if "cache" in props or "cache" in self_props:
if (
expiration
and timestamp
and ("expire" in props or "expire" in self_props)
):
2023-05-11 15:44:48 +02:00
ntime = int(time())
2024-04-24 15:39:17 +02:00
if timestamp + subconfig.config_bag.expiration_time >= ntime:
2023-05-11 15:44:48 +02:00
return True, value, validated
else:
return True, value, validated
return no_cache
2024-10-31 08:53:58 +01:00
def setcache(
self,
subconfig,
val,
type_="values",
validated=True,
):
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
"""
2024-10-31 08:53:58 +01:00
if type_ == "values":
if (
"cache" not in subconfig.config_bag.properties
and "cache" not in subconfig.properties
):
2023-05-11 15:44:48 +02:00
return
2024-10-31 08:53:58 +01:00
elif (
subconfig is None or "cache" not in subconfig.config_bag.properties
) and "cache" not in val:
2023-05-11 15:44:48 +02:00
return
2024-04-24 15:39:17 +02:00
path, index = self._get_path_index(subconfig)
2023-05-11 15:44:48 +02:00
self._cache.setdefault(path, {})[index] = (val, int(time()), validated)
def delcache(self, path):
2024-10-31 08:53:58 +01:00
"""reset cache a a specified path"""
if path in self._cache:
del self._cache[path]
def get_cached(self):
2024-10-31 08:53:58 +01:00
"""get cache values"""
return self._cache
def reset_all_cache(self):
2024-10-31 08:53:58 +01:00
"""reset all cache values"""
self._cache.clear()