Commit 60c5c0f2 authored by Sean Bleier's avatar Sean Bleier
parents f795977c f5554488
......@@ -403,3 +403,26 @@ class BaseRedisCache(BaseCache):
Reinsert cache entries using the current pickle protocol version.
"""
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
......@@ -211,3 +211,13 @@ class ShardedRedisCache(BaseRedisCache):
for client in self.clients.itervalues():
self._reinsert_keys(client)
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):
Reinsert cache entries using the current pickle protocol version.
"""
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(SetupMixin):
"""
ttl = self.cache.ttl('does_not_exist')
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