Skip to content

Commit

Permalink
Patch potential errors when running memory cache in a thread
Browse files Browse the repository at this point in the history
  • Loading branch information
huntfx committed Feb 12, 2021
1 parent af3eeba commit 8273165
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion supercache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .utils import *


__version__ = '2.0.0'
__version__ = '2.0.1'


class Memoize(object):
Expand Down
43 changes: 30 additions & 13 deletions supercache/engine/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Memory(object):
"""Cache directly in memory.
This is by far the fastest solution, but the cache cannot be shared
outside the current process.
This is not completely thread safe, but care has been taken to
avoid any errors from stopping the code working.
"""

FIFO = FirstInFirstOut = 0
Expand Down Expand Up @@ -74,7 +76,10 @@ def expired(self, key, _current_time=None):
return False
if _current_time is None:
_current_time = time.time()
return self.data['ttl'][key] <= _current_time
try:
return self.data['ttl'][key] <= _current_time
except KeyError:
return True

def get(self, key, purge=False):
"""Get the value belonging to a key.
Expand All @@ -91,10 +96,13 @@ def get(self, key, purge=False):
if not purge and self.expired(key):
raise exceptions.CacheExpired(key)

self.data['hits'][key] += 1
self.data['access'][key] = time.time()
try:
self.data['hits'][key] += 1
self.data['access'][key] = time.time()

return self.data['result'][key]
return self.data['result'][key]
except KeyError:
raise exceptions.CacheExpired(key)

def put(self, key, value, ttl=None, purge=True):
"""Add a new value to cache.
Expand All @@ -104,7 +112,10 @@ def put(self, key, value, ttl=None, purge=True):
ttl = self.ttl

self.data['result'][key] = value
self.data['misses'][key] += 1
try:
self.data['misses'][key] += 1
except KeyError:
self.data['misses'][key] = 1

# Calculate size
if self.size is not None:
Expand Down Expand Up @@ -135,13 +146,16 @@ def delete(self, key):
This will not remove the hits or misses.
"""
if key in self.data['result']:
del self.data['result'][key]
del self.data['insert'][key]
del self.data['access'][key]
if key in self.data['ttl']:
del self.data['ttl'][key]
if self.size is not None:
self.data['size'][None] -= self.data['size'].pop(key)
try:
del self.data['result'][key]
del self.data['insert'][key]
del self.data['access'][key]
if key in self.data['ttl']:
del self.data['ttl'][key]
if self.size is not None:
self.data['size'][None] -= self.data['size'].pop(key)
except KeyError:
pass
return True
return False

Expand All @@ -168,7 +182,10 @@ def _purge(self, ignore=None):
if self.expired(key, _current_time=current_time):
self.delete(key)
elif key in self.data['ttl']:
self._next_ttl = min(self._next_ttl, self.data['ttl'][key])
try:
self._next_ttl = min(self._next_ttl, self.data['ttl'][key])
except KeyError:
pass

# Determine if we can skip
if count is not None and len(self.data['result']) < count:
Expand Down

0 comments on commit 8273165

Please sign in to comment.