Ticket #12399: memcached-timeout-fixes.diff
File memcached-timeout-fixes.diff, 2.1 KB (added by , 15 years ago) |
---|
-
django/core/cache/backends/memcached.py
1 1 "Memcached cache backend" 2 2 3 import time 4 3 5 from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError 4 6 from django.utils.encoding import smart_unicode, smart_str 5 7 … … 16 18 BaseCache.__init__(self, params) 17 19 self._cache = memcache.Client(server.split(';')) 18 20 21 def _get_memcache_timeout(self, timeout): 22 """ 23 Memcached has peculiarities in the way it deals with timeouts. 24 Call this function to obtain a safe value for your timeout. 25 """ 26 timeout = timeout or self.default_timeout 27 if timeout > 2592000: # 60*60*24*30, 30 days 28 # See http://code.google.com/p/memcached/wiki/FAQ 29 # "You can set expire times up to 30 days in the future. After that 30 # memcached interprets it as a date, and will expire the item after 31 # said date. This is a simple (but obscure) mechanic." 32 # 33 # This means that we have to switch to absolute timestamps. 34 timeout += int(time.time()) 35 return timeout 36 19 37 def add(self, key, value, timeout=0): 20 38 if isinstance(value, unicode): 21 39 value = value.encode('utf-8') 22 return self._cache.add(smart_str(key), value, timeout or self.default_timeout)40 return self._cache.add(smart_str(key), value, self._get_memcache_timeout(timeout)) 23 41 24 42 def get(self, key, default=None): 25 43 val = self._cache.get(smart_str(key)) … … 34 52 def set(self, key, value, timeout=0): 35 53 if isinstance(value, unicode): 36 54 value = value.encode('utf-8') 37 self._cache.set(smart_str(key), value, timeout or self.default_timeout)55 self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout)) 38 56 39 57 def delete(self, key): 40 58 self._cache.delete(smart_str(key))