diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2010-02-11 12:06:26 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2010-02-11 12:06:26 +0000 |
| commit | 75ab212d12058494eaf171acfc762206d8a62d47 (patch) | |
| tree | f0d731e709e14a2b480d3b636f2a3ad40636d44f /django | |
| parent | be57541af11b7144de700978b9ea3b50f20ffd02 (diff) | |
Fixed #12399 -- Added handling for memcache timeouts longer than 30 days. Thanks to houdinihound for the report, and gciotta for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12408 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/cache/backends/memcached.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py index ca45fc99fa..e254527cea 100644 --- a/django/core/cache/backends/memcached.py +++ b/django/core/cache/backends/memcached.py @@ -1,5 +1,7 @@ "Memcached cache backend" +import time + from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError from django.utils.encoding import smart_unicode, smart_str @@ -16,10 +18,26 @@ class CacheClass(BaseCache): BaseCache.__init__(self, params) self._cache = memcache.Client(server.split(';')) + def _get_memcache_timeout(self, timeout): + """ + Memcached deals with long (> 30 days) timeouts in a special + way. Call this function to obtain a safe value for your timeout. + """ + timeout = timeout or self.default_timeout + if timeout > 2592000: # 60*60*24*30, 30 days + # See http://code.google.com/p/memcached/wiki/FAQ + # "You can set expire times up to 30 days in the future. After that + # memcached interprets it as a date, and will expire the item after + # said date. This is a simple (but obscure) mechanic." + # + # This means that we have to switch to absolute timestamps. + timeout += int(time.time()) + return timeout + def add(self, key, value, timeout=0): if isinstance(value, unicode): value = value.encode('utf-8') - return self._cache.add(smart_str(key), value, timeout or self.default_timeout) + return self._cache.add(smart_str(key), value, self._get_memcache_timeout(timeout)) def get(self, key, default=None): val = self._cache.get(smart_str(key)) @@ -34,7 +52,7 @@ class CacheClass(BaseCache): def set(self, key, value, timeout=0): if isinstance(value, unicode): value = value.encode('utf-8') - self._cache.set(smart_str(key), value, timeout or self.default_timeout) + self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout)) def delete(self, key): self._cache.delete(smart_str(key)) @@ -78,7 +96,7 @@ class CacheClass(BaseCache): if isinstance(value, unicode): value = value.encode('utf-8') safe_data[smart_str(key)] = value - self._cache.set_multi(safe_data, timeout or self.default_timeout) + self._cache.set_multi(safe_data, self._get_memcache_timeout(timeout)) def delete_many(self, keys): self._cache.delete_multi(map(smart_str, keys)) |
