Skip to content

Commit

Permalink
Add django >= 1.6 timeout=None compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Reggers authored and Andrey Antukh committed Dec 15, 2013
1 parent 0bc71e3 commit 09939dc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions redis_cache/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def setup_pickle_version(self):
except (ValueError, TypeError):
raise ImproperlyConfigured("PICKLE_VERSION value must be an integer")

def set(self, key, value, timeout=None, version=None, client=None, nx=False):
def set(self, key, value, timeout=True, version=None, client=None, nx=False):
"""
Persist a value to the cache, and set an optional expiration time.
Also supports optional nx parameter. If set to True - will use redis setnx instead of set.
Expand All @@ -135,17 +135,17 @@ def set(self, key, value, timeout=None, version=None, client=None, nx=False):
key = self.make_key(key, version=version)
value = self.pickle(value)

if timeout is None:
if timeout is True:
timeout = self._backend.default_timeout

try:
if nx:
res = client.setnx(key, value)
if res and timeout > 0:
if res and timeout is not None and timeout > 0:
return client.expire(key, int(timeout))
return res
else:
if timeout > 0:
if timeout is not None and timeout > 0:
return client.setex(key, value, int(timeout))
return client.set(key, value)
except ConnectionError:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
url = "https://github.com/niwibe/django-redis",
author = "Andrei Antoukh",
author_email = "niwi@niwi.be",
version='3.3',
version='3.3.1',
packages = [
"redis_cache",
"redis_cache.client",
Expand Down
6 changes: 5 additions & 1 deletion tests/redis_backend_testapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def test_setnx(self):
res = self.cache.get("test_key_nx", None)
self.assertEqual(res, None)


def test_setnx_timeout(self):
# test that timeout still works for nx=True
res = self.cache.set("test_key_nx", 1, timeout=2, nx=True)
Expand Down Expand Up @@ -117,6 +116,11 @@ def test_timeout(self):
res = self.cache.get("test_key", None)
self.assertEqual(res, None)

def test_timeout_0(self):
self.cache.set("test_key", 222, timeout=0)
res = self.cache.get("test_key", None)
self.assertEqual(res, 222)

def test_set_add(self):
self.cache.set('add_key', 'Initial value')
self.cache.add('add_key', 'New value')
Expand Down

0 comments on commit 09939dc

Please sign in to comment.