PageRenderTime 15ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/johnny/backends/memcached.py

https://bitbucket.org/jmoiron/johnny-cache/
Python | 47 lines | 25 code | 0 blank | 22 comment | 2 complexity | 064d54b6bc5ba106637a2fe24a3137ca MD5 | raw file
 1"""
 2Infinite caching memcached class.  Caches forever when passed a timeout
 3of 0.  For Django >= 1.3, this module also provides ``MemcachedCache`` and
 4``PyLibMCCache``, which use the backends of their respective analogs in
 5django's default backend modules.
 6"""
 7
 8import django
 9from django.core.cache.backends import memcached
10
11
12class CacheClass(memcached.CacheClass):
13    """
14    By checking ``timeout is None`` rather than ``not timeout``, this
15    cache class allows for non-expiring cache writes on certain backends,
16    notably memcached.
17    """
18    def _get_memcache_timeout(self, timeout=None):
19        if timeout == 0:
20            return 0  # 2591999
21        return super(CacheClass, self)._get_memcache_timeout(timeout)
22
23if django.VERSION[:2] > (1, 2):
24
25    class MemcachedCache(memcached.MemcachedCache):
26        """
27        Infinitely Caching version of django's MemcachedCache backend.
28        """
29        def _get_memcache_timeout(self, timeout=None):
30            if timeout == 0:
31                return 0  # 2591999
32            return super(MemcachedCache, self)._get_memcache_timeout(timeout)
33
34    class PyLibMCCache(memcached.PyLibMCCache):
35        """
36        PyLibMCCache version that interprets 0 to mean, roughly, 30 days.
37        This is because `pylibmc interprets 0 to mean literally zero seconds
38        <http://sendapatch.se/projects/pylibmc/misc.html#differences-from-python-memcached>`_
39        rather than "infinity" as memcached itself does.  The maximum timeout
40        memcached allows before treating the timeout as a timestamp is just
41        under 30 days.
42        """
43        def _get_memcache_timeout(self, timeout=None):
44            # pylibmc doesn't like our definition of 0
45            if timeout == 0:
46                return 2591999
47            return super(PyLibMCCache, self)._get_memcache_timeout(timeout)