Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache handles stale values #22

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions pycrest/eve.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


class APICache(object):
def put(self, key, value):
def put(self, key, value, ex):
raise NotImplementedError

def get(self, key):
Expand All @@ -50,9 +50,9 @@ def __init__(self, path):
def _getpath(self, key):
return os.path.join(self.path, str(hash(key)) + '.cache')

def put(self, key, value):
def put(self, key, value, ex):
with open(self._getpath(key), 'wb') as f:
f.write(zlib.compress(pickle.dumps(value, -1)))
f.write(zlib.compress(pickle.dumps((value, ex + time.time()), -1)))
self._cache[key] = value

def get(self, key):
Expand All @@ -61,7 +61,14 @@ def get(self, key):

try:
with open(self._getpath(key), 'rb') as f:
return pickle.loads(zlib.decompress(f.read()))
ret = pickle.loads(zlib.decompress(f.read()))
if ret[1] > time.time():
return ret[0]

# if we are here, we got stale cache, so we can invalidate it
self.invalidate(key)
return None

except IOError as ex:
if ex.errno == 2: # file does not exist (yet)
return None
Expand All @@ -85,10 +92,20 @@ def __init__(self):
self._dict = {}

def get(self, key):
return self._dict.get(key, None)
ret = self._dict.get(key, None)
if ret is not None and ret[1] > time.time():
# cache hit
return ret[0]
elif ret is None:
# cache miss
return None
else:
# stale, delete from cache
self.invalidate(key)
return None

def put(self, key, value):
self._dict[key] = value
def put(self, key, value, ex):
self._dict[key] = value, ex + time.time()

def invalidate(self, key):
self._dict.pop(key, None)
Expand Down Expand Up @@ -141,12 +158,9 @@ def get(self, resource, params=None):
# check cache
key = (resource, frozenset(self._session.headers.items()), frozenset(prms.items()))
cached = self.cache.get(key)
if cached and cached['expires'] > time.time():
if cached:
logger.debug('Cache hit for resource %s (params=%s)', resource, prms)
return cached['payload']
elif cached:
logger.debug('Cache stale for resource %s (params=%s)', resource, prms)
self.cache.invalidate(key)
return cached
else:
logger.debug('Cache miss for resource %s (params=%s', resource, prms)

Expand All @@ -161,7 +175,7 @@ def get(self, resource, params=None):
key = (resource, frozenset(self._session.headers.items()), frozenset(prms.items()))
expires = self._get_expires(res)
if expires > 0:
self.cache.put(key, {'expires': time.time() + expires, 'payload': ret})
self.cache.put(key, ret, expires)

return ret

Expand Down
12 changes: 6 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_pagination(url, request):
recf = open(path, 'r')
rec = pickle.loads(zlib.decompress(recf.read()))
recf.close()
rec['expires'] = 1
rec = rec[0], 1

recf = open(path, 'w')
recf.write(zlib.compress(pickle.dumps(rec)))
Expand Down Expand Up @@ -436,7 +436,7 @@ def token_mock(url, request):
recf = open(path, 'r')
rec = pickle.loads(zlib.decompress(recf.read()))
recf.close()
rec['expires'] = 1
rec = rec[0], 1

recf = open(path, 'w')
recf.write(zlib.compress(pickle.dumps(rec)))
Expand All @@ -462,15 +462,15 @@ def test_apicache(self, mock_open, mock_unlink, mock_mkdir, mock_isdir):
# Just because pragma: no cover is ugly
cache = pycrest.eve.APICache()
self.assertRaises(NotImplementedError, lambda: cache.get("foo"))
self.assertRaises(NotImplementedError, lambda: cache.put("foo", "bar"))
self.assertRaises(NotImplementedError, lambda: cache.put("foo", "bar", time.time()))
self.assertRaises(NotImplementedError, lambda: cache.invalidate("foo"))

# Test default DictCache
crest = pycrest.EVE()
self.assertEqual(type(crest.cache).__name__, "DictCache")
crest.cache.invalidate('nxkey')
self.assertEqual(crest.cache.get('nxkey'), None)
crest.cache.put('key', 'value')
crest.cache.put('key', 'value', time.time() + 1)
self.assertEqual(crest.cache.get('key'), 'value')


Expand All @@ -490,7 +490,7 @@ def test_apicache(self, mock_open, mock_unlink, mock_mkdir, mock_isdir):
self.assertEqual(crest.cache.get('nxkey'), None)

# cache (key, value) pair and retrieve it
crest.cache.put('key', 'value')
crest.cache.put('key', 'value', time.time() + 1)
self.assertEqual(crest.cache.get('key'), 'value')

# retrieve from disk
Expand Down Expand Up @@ -633,4 +633,4 @@ def brokenInt(url, request):
with mock.patch('time.time') as mock_time:
mock_time.return_value = 0
eve.shouldCache()
self.assertEqual(list(eve.cache._dict.items())[0][1]['expires'], 300)
self.assertEqual(list(eve.cache._dict.items())[0][1][1], 300)