Ticket #12399: memcached-timeout-fixes.diff

File memcached-timeout-fixes.diff, 2.1 KB (added by gciotta, 15 years ago)

Fixes at cache layer, tested on 1.1.1 and trunk r12157

  • django/core/cache/backends/memcached.py

     
    11"Memcached cache backend"
    22
     3import time
     4
    35from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
    46from django.utils.encoding import smart_unicode, smart_str
    57
     
    1618        BaseCache.__init__(self, params)
    1719        self._cache = memcache.Client(server.split(';'))
    1820
     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
    1937    def add(self, key, value, timeout=0):
    2038        if isinstance(value, unicode):
    2139            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))
    2341
    2442    def get(self, key, default=None):
    2543        val = self._cache.get(smart_str(key))
     
    3452    def set(self, key, value, timeout=0):
    3553        if isinstance(value, unicode):
    3654            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))
    3856
    3957    def delete(self, key):
    4058        self._cache.delete(smart_str(key))
Back to Top