Commit 8ee175f5 authored by sebleier's avatar sebleier

Made it possible to have non-expiring keys

Thanks to Kami for the report and Jezdez for being awesome.
parent 5a8ab033
......@@ -85,9 +85,17 @@ class CacheClass(BaseCache):
"""
Set content expiration, if necessary
"""
if timeout is None:
timeout = self.default_timeout
self._cache.expire(key, timeout)
if timeout == 0:
# force the key to be non-volatile
result = self._cache.get(key)
self._cache.set(key, result)
else:
timeout = timeout or self.default_timeout
# If the expiration command returns false, we need to reset the key
# with the new expiration
if not self._cache.expire(key, timeout):
value = self.get(key)
self.set(key, value, timeout)
def delete(self, key):
"""
......
......@@ -3,6 +3,11 @@
import time
import unittest
try:
import cPickle as pickle
except ImportError:
import pickle
from django.core.cache import get_cache
from models import Poll, expensive_calculation
......@@ -160,6 +165,40 @@ class RedisCacheTests(unittest.TestCase):
self.assertEqual(self.cache.get("expire2"), "newvalue")
self.assertEqual(self.cache.has_key("expire3"), False)
def test_set_expiration_timeout_None(self):
key, value = 'key', 'value'
self.cache.set(key, value);
self.assertTrue(self.cache._cache.ttl(key) > 0)
def test_set_expiration_timeout_0(self):
key, value = 'key', 'value'
self.cache.set(key, value);
self.assertTrue(self.cache._cache.ttl(key) > 0)
self.cache.expire(key, 0)
self.assertEqual(self.cache.get(key), value)
self.assertTrue(self.cache._cache.ttl(key) < 0)
def test_set_expiration_first_expire_call(self):
key, value = self.cache.prepare_key('key'), 'value'
# bypass public set api so we don't set the expiration
self.cache._cache.set(key, pickle.dumps(value))
self.cache.expire(key, 1)
time.sleep(2)
self.assertEqual(self.cache.get('key'), None)
def test_set_expiration_mulitple_expire_calls(self):
key, value = 'key', 'value'
self.cache.set(key, value, 1)
time.sleep(2)
self.assertEqual(self.cache.get('key'), None)
self.cache.set(key, value, 100)
self.assertEqual(self.cache.get('key'), value)
time.sleep(2)
self.assertEqual(self.cache.get('key'), value)
self.cache.expire(key, 1)
time.sleep(2)
self.assertEqual(self.cache.get('key'), None)
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