Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the way we handle default timeout for Django 1.6+
- Loading branch information
Showing
2 changed files
with
16 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 = ( | ||
|
@@ -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. | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
twidi
Contributor
|
||
timeout = self.default_timeout | ||
|
||
# If ``value`` is not an int, then pickle it | ||
|
@@ -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. | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@thoas Wouldn't this be wrong if
DEFAULT_TIMEOUT
is not None? Comparing to numbers or strings should be done using equality operators and notis
.