Files
Sheerka-Old/src/cache/ListCache.py
T
2021-01-11 15:36:03 +01:00

66 lines
2.4 KiB
Python

from cache.Cache import BaseCache
from core.global_symbols import Removed, NotFound
from core.utils import sheerka_deepcopy
class ListCache(BaseCache):
"""
An in memory FIFO cache object
When the max_size is reach the first element that was put is removed
Items of this cache are list
"""
def _put(self, key, value, alt_sdp):
if key in self._cache:
self._cache[key].append(value)
else:
self._sync(key)
if key not in self._cache and alt_sdp and not self._is_cleared:
previous = self._alt_sdp_get(alt_sdp, key)
if previous not in (NotFound, Removed):
self._cache[key] = sheerka_deepcopy(previous)
if key in self._cache:
self._cache[key].append(value)
else:
self._cache[key] = [value]
self._add_to_add(key)
return True
def _update(self, old_key, old_value, new_key, new_value, alt_sdp):
self._sync(old_key, new_key)
if old_key not in self._cache and alt_sdp and not self._is_cleared:
# no value found in local cache or remote repository
# Use the values from alt_sdp
previous = self._alt_sdp_get(alt_sdp, old_key)
if previous in (NotFound, Removed):
raise KeyError(old_key)
self._cache[old_key] = sheerka_deepcopy(previous)
self._current_size += len(previous)
if old_key != new_key:
self._cache[old_key].remove(old_value)
if len(self._cache[old_key]) == 0:
if not self._is_cleared and alt_sdp and self._extend_exists(alt_sdp, old_key):
self._cache[old_key] = Removed
self._add_to_add(old_key)
self._current_size += 1
else:
del (self._cache[old_key])
self._add_to_remove(old_key)
else:
self._add_to_add(old_key)
self._put(new_key, new_value, alt_sdp)
self._add_to_add(new_key)
else:
for i in range(len(self._cache[new_key])):
if self._cache[new_key][i] == old_value:
self._cache[new_key][i] = new_value # avoid add and remove in dict
break # only the first one is affected
self._add_to_add(new_key)