/johnny/settings.py

https://bitbucket.org/jmoiron/johnny-cache/ · Python · 54 lines · 35 code · 11 blank · 8 comment · 10 complexity · 8bfbec4959ea88e8e18827a83419fc8c MD5 · raw file

  1. from warnings import warn
  2. import django
  3. from django.conf import settings
  4. from django.core.cache import get_cache, cache
  5. DISABLE_QUERYSET_CACHE = getattr(settings, 'DISABLE_QUERYSET_CACHE', False)
  6. BLACKLIST = getattr(settings, 'MAN_IN_BLACKLIST',
  7. getattr(settings, 'JOHNNY_TABLE_BLACKLIST', []))
  8. BLACKLIST = set(BLACKLIST)
  9. WHITELIST = set(getattr(settings, 'JOHNNY_TABLE_WHITELIST', []))
  10. if django.VERSION[1] >= 2:
  11. DB_CACHE_KEYS = dict((name, db.get('JOHNNY_CACHE_KEY', name))
  12. for name, db in settings.DATABASES.iteritems())
  13. else:
  14. DB_CACHE_KEYS = {'default': 'default'}
  15. MIDDLEWARE_KEY_PREFIX = getattr(settings, 'JOHNNY_MIDDLEWARE_KEY_PREFIX', 'jc')
  16. MIDDLEWARE_SECONDS = getattr(settings, 'JOHNNY_MIDDLEWARE_SECONDS', 0)
  17. CACHE_BACKEND = getattr(settings, 'JOHNNY_CACHE_BACKEND',
  18. getattr(settings, 'CACHE_BACKEND', None))
  19. CACHES = getattr(settings, 'CACHES', {})
  20. def _get_backend():
  21. """
  22. Returns the actual django cache object johnny is configured to use.
  23. This relies on the settings only; the actual active cache can
  24. theoretically be changed at runtime.
  25. """
  26. enabled = [n for n, c in sorted(CACHES.items())
  27. if c.get('JOHNNY_CACHE', False)]
  28. if len(enabled) > 1:
  29. warn("Multiple caches configured for johnny-cache; using %s." %
  30. enabled[0])
  31. if enabled:
  32. return get_cache(enabled[0])
  33. if CACHE_BACKEND:
  34. backend = get_cache(CACHE_BACKEND)
  35. if backend not in CACHES:
  36. from django.core import signals
  37. # Some caches -- python-memcached in particular -- need to do a
  38. # cleanup at the end of a request cycle. If the cache provides a
  39. # close() method, wire it up here.
  40. if hasattr(backend, 'close'):
  41. signals.request_finished.connect(backend.close)
  42. return backend
  43. return cache