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