Commit f5554488 authored by Michael Lindemuth's avatar Michael Lindemuth

Adds custom expire and persist methods that use Redis' expire and persist methods

Better than getting and resetting keys in cache to change timeout
parent afe401a2
...@@ -418,3 +418,26 @@ class BaseRedisCache(BaseCache): ...@@ -418,3 +418,26 @@ class BaseRedisCache(BaseCache):
Reinsert cache entries using the current pickle protocol version. Reinsert cache entries using the current pickle protocol version.
""" """
raise NotImplementedError raise NotImplementedError
def _persist(self, client, key, version=None):
if client.exists(key):
client.persist(key)
def persist(self, key):
"""
Remove the timeout on a key. Equivalent to setting a timeout
of None in a set command.
"""
raise NotImplementedError
def _expire(self, client, key, timeout, version=None):
if client.exists(key):
client.expire(key, timeout)
def expire(self, key, timeout):
"""
Set the expire time on a key
Will raise an exception if the key does not exist
"""
raise NotImplementedError
...@@ -222,3 +222,13 @@ class ShardedRedisCache(BaseRedisCache): ...@@ -222,3 +222,13 @@ class ShardedRedisCache(BaseRedisCache):
for client in self.clients.itervalues(): for client in self.clients.itervalues():
self._reinsert_keys(client) self._reinsert_keys(client)
print print
def persist(self, key, version=None):
client = self.get_client(key, for_write=True)
key = self.make_key(key, version=version)
self._persist(client, key, version)
def expire(self, key, timeout, version=None):
client = self.get_client(key, for_write=True)
key = self.make_key(key, version=version)
self._expire(client, key, timeout, version)
...@@ -151,3 +151,11 @@ class RedisCache(BaseRedisCache): ...@@ -151,3 +151,11 @@ class RedisCache(BaseRedisCache):
Reinsert cache entries using the current pickle protocol version. Reinsert cache entries using the current pickle protocol version.
""" """
self._reinsert_keys(self.client) self._reinsert_keys(self.client)
def persist(self, key, version=None):
key = self.make_key(key, version=version)
self._persist(self.client, key, version)
def expire(self, key, timeout, version=None):
key = self.make_key(key, version=version)
self._expire(self.client, key, timeout, version)
...@@ -470,3 +470,27 @@ class BaseRedisTestCase(object): ...@@ -470,3 +470,27 @@ class BaseRedisTestCase(object):
""" """
ttl = self.cache.ttl('does_not_exist') ttl = self.cache.ttl('does_not_exist')
self.assertEqual(ttl, 0) self.assertEqual(ttl, 0)
def test_persist_expire_to_persist(self):
self.cache.set('a', 'a', timeout=10)
self.cache.persist('a')
ttl = self.cache.ttl('a')
self.assertEqual(ttl, None)
def test_expire_no_expiry_to_expire(self):
self.cache.set('a', 'a', timeout=None)
self.cache.expire('a', 10)
ttl = self.cache.ttl('a')
self.assertAlmostEqual(ttl, 10)
def test_expire_less(self):
self.cache.set('a', 'a', timeout=20)
self.cache.expire('a', 10)
ttl = self.cache.ttl('a')
self.assertAlmostEqual(ttl, 10)
def test_expire_more(self):
self.cache.set('a', 'a', timeout=10)
self.cache.expire('a', 20)
ttl = self.cache.ttl('a')
self.assertAlmostEqual(ttl, 20)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment