Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __getitem__(self, key):
self._key_check(key)
try:
value = Cache.__getitem__(self, key)
except KeyError:
try:
value = self._second_gen[key]
except KeyError:
try:
value = self._get_item(key)
except KeyError as ke3:
raise ke3
else:
self.__setitem__(key, value)
else:
self.__setitem__(key, value)
if key in self._second_gen: # the second gen clean up could be triggered during set in first gen
del self._second_gen[key]
else:
self._update_order(key)
def sync(self):
for key in iter(self):
value = cachetools.Cache.__getitem__(self, key)
self._dbset(key, value)
def __getitem__(self, key, cache_getitem=Cache.__getitem__):
try:
link = self.__getlink(key)
except KeyError:
expired = False
else:
expired = link.expire < self.__timer()
if expired:
return self.__missing__(key)
else:
return cache_getitem(self, key)
def popitem(self):
"""Remove and return the `(key, value)` pair least frequently used."""
items = ((key, Cache.__getitem__(self, key)[1]) for key in self)
try:
key, _ = min(items, key=operator.itemgetter(1))
except ValueError:
raise KeyError('cache is empty')
return (key, self.pop(key))
def __repr__(self, cache_getitem=Cache.__getitem__):
return '%s(%r, maxsize=%d, currsize=%d)' % (
self.__class__.__name__,
[(key, cache_getitem(self, key)[0]) for key in self],
self.maxsize,
self.currsize,
)
def __getitem__(self, key, cache_getitem=Cache.__getitem__):
value = cache_getitem(self, key)
self._update_order(key)
return value