Commit 5441d95f authored by Sean Bleier's avatar Sean Bleier

Modified the timeout value semantics to better match the memcache cache backend,…

Modified the timeout value semantics to better match the memcache cache backend, i.e. negative timeouts are instant expiration, 0 timeout is no expiration, and greater than 0 is time to live.
parent 1cfb3633
......@@ -184,6 +184,13 @@ class CacheClass(BaseCache):
result = self.unpickle(value)
return result
def _set(self, key, value, timeout, client):
if timeout == 0:
return client.set(key, value)
elif timeout > 0:
return client.setex(key, value, int(timeout))
else:
return False
def set(self, key, value, timeout=None, version=None, client=None):
"""
......@@ -192,14 +199,14 @@ class CacheClass(BaseCache):
if not client:
client = self._client
key = self.make_key(key, version=version)
if not timeout:
if timeout is None:
timeout = self.default_timeout
try:
value = int(value)
except (ValueError, TypeError):
result = client.setex(key, pickle.dumps(value), int(timeout))
result = self._set(key, pickle.dumps(value), int(timeout), client)
else:
result = client.setex(key, value, int(timeout))
result = self._set(key, value, int(timeout), client)
# result is a boolean
return result
......
......@@ -213,9 +213,21 @@ class RedisCacheTests(TestCase):
def test_set_expiration_timeout_None(self):
key, value = self.cache.make_key('key'), 'value'
self.cache.set(key, value);
self.cache.set(key, value)
self.assertTrue(self.cache._client.ttl(key) > 0)
def test_set_expiration_timeout_zero(self):
key, value = self.cache.make_key('key'), 'value'
self.cache.set(key, value, timeout=0)
self.assertTrue(self.cache._client.ttl(key) is None)
self.assertTrue(self.cache.has_key(key))
def test_set_expiration_timeout_negative(self):
key, value = self.cache.make_key('key'), 'value'
self.cache.set(key, value, timeout=-1)
self.assertTrue(self.cache._client.ttl(key) is None)
self.assertFalse(self.cache.has_key(key))
def test_unicode(self):
# Unicode values can be cached
stuff = {
......
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