cache: new module for utility methods with cache

First function is get_shared_cache(), it returns an instance of a shared
if it can or raise ImproperlyConfigured.
This commit is contained in:
Benjamin Dauvergne 2013-11-07 17:35:09 +01:00
parent 842cd91598
commit 8372083053
1 changed files with 25 additions and 0 deletions

25
authentic2/cache.py Normal file
View File

@ -0,0 +1,25 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.cache import InvalidCacheBackendError, cache, get_cache
from django.core.cache.backends.locmem import LocMemCache
from django.core.cache.backends.dummy import DummyCache
def get_shared_cache(name=None):
'''Try to return a cache backend shared between requests. Fail by raising
ImproperlyConfigured.
'''
candidate = cache
try:
candidate = get_cache('persistent')
except InvalidCacheBackendError:
pass
if name is not None:
try:
candidate = get_cache(name)
except InvalidCacheBackendError:
pass
if type(cache) in (DummyCache, LocMemCache):
raise ImproperlyConfigured('no shared cache backend is configured for %r' % name)
return cache