Skip to content

Commit

Permalink
Change the way we handle default timeout for Django 1.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Jan 4, 2014
1 parent 1663885 commit c2ccc74
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 9 additions & 8 deletions redis_cache/cache.py
Expand Up @@ -2,7 +2,8 @@
from django.core.exceptions import ImproperlyConfigured
from django.utils import importlib
from django.utils.datastructures import SortedDict
from .compat import smart_text, smart_bytes, bytes_type, python_2_unicode_compatible
from .compat import (smart_text, smart_bytes, bytes_type,
python_2_unicode_compatible, DEFAULT_TIMEOUT)

try:
import cPickle as pickle
Expand Down Expand Up @@ -45,8 +46,8 @@ def __init__(self):
self._connection_pools = {}

def get_connection_pool(self, host='127.0.0.1', port=6379, db=1,
password=None, parser_class=None,
unix_socket_path=None):
password=None, parser_class=None,
unix_socket_path=None):
connection_identifier = (host, port, db, parser_class, unix_socket_path)
if not self._connection_pools.get(connection_identifier):
connection_class = (
Expand Down Expand Up @@ -162,7 +163,7 @@ def make_key(self, key, version=None):
key = CacheKey(key)
return key

def add(self, key, value, timeout=None, version=None):
def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
"""
Add a value to the cache, failing if the key already exists.
Expand Down Expand Up @@ -201,14 +202,14 @@ def _set(self, key, value, timeout, client, _add_only=False):
else:
return False

def set(self, key, value, timeout=None, version=None, client=None, _add_only=False):
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None, client=None, _add_only=False):
"""
Persist a value to the cache, and set an optional expiration time.
"""
if not client:
client = self._client
key = self.make_key(key, version=version)
if timeout is None:
if timeout is DEFAULT_TIMEOUT:

This comment has been minimized.

Copy link
@glarrain

glarrain Jan 27, 2014

@thoas Wouldn't this be wrong if DEFAULT_TIMEOUT is not None? Comparing to numbers or strings should be done using equality operators and not is.

This comment has been minimized.

Copy link
@twidi

twidi Jun 27, 2014

Contributor

DEFAULT_TIMEOUT will be object() (django 1.6+) or None. It allows passing None to say illimited timeout instead of None to say "use the default timeout", which is 300s.

timeout = self.default_timeout

# If ``value`` is not an int, then pickle it
Expand Down Expand Up @@ -269,7 +270,7 @@ def get_many(self, keys, version=None):
recovered_data[map_keys[key]] = value
return recovered_data

def set_many(self, data, timeout=None, version=None):
def set_many(self, data, timeout=DEFAULT_TIMEOUT, version=None):
"""
Set a bunch of values in the cache at once from a dict of key/value
pairs. This is much more efficient than calling set() multiple times.
Expand Down Expand Up @@ -324,7 +325,7 @@ def incr_version(self, key, delta=1, version=None):
ttl = self._client.ttl(old_key)
if value is None:
raise ValueError("Key '%s' not found" % key)
new_key = self.make_key(key, version=version+delta)
new_key = self.make_key(key, version=version + delta)
# TODO: See if we can check the version of Redis, since 2.2 will be able
# to rename volitile keys.
self.set(new_key, value, timeout=ttl)
Expand Down
7 changes: 7 additions & 0 deletions redis_cache/compat.py
@@ -1,4 +1,5 @@
import sys
import django

PY3 = (sys.version_info >= (3,))

Expand All @@ -17,6 +18,12 @@
else:
bytes_type = str

if django.VERSION[:2] >= (1, 6):
from django.core.cache.backends.base import DEFAULT_TIMEOUT as DJANGO_DEFAULT_TIMEOUT
DEFAULT_TIMEOUT = DJANGO_DEFAULT_TIMEOUT
else:
DEFAULT_TIMEOUT = None


def python_2_unicode_compatible(klass):
"""
Expand Down

0 comments on commit c2ccc74

Please sign in to comment.