PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/johnny/settings.py

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