diff --git a/nexus_memcache/conf.py b/nexus_memcache/conf.py index a1066df..fe1c77b 100644 --- a/nexus_memcache/conf.py +++ b/nexus_memcache/conf.py @@ -1,3 +1,4 @@ +import django from django.conf import settings BACKEND = getattr(settings, 'NEXUS_MEMCACHE_BACKEND', getattr(settings, 'CACHE_BACKEND', None)) diff --git a/nexus_memcache/nexus_modules.py b/nexus_memcache/nexus_modules.py index df10999..2ccc3c0 100644 --- a/nexus_memcache/nexus_modules.py +++ b/nexus_memcache/nexus_modules.py @@ -1,8 +1,13 @@ import socket import warnings +import django from django.core.cache import get_cache, parse_backend_uri +if django.get_version() >= "1.3": + from django.core.cache import parse_backend_conf +from django.core.cache.backends.base import InvalidCacheBackendError from django.utils.datastructures import SortedDict +from django.utils import importlib import nexus @@ -14,12 +19,28 @@ class MemcacheModule(nexus.NexusModule): def get_caches(self): caches = [] - schema, hosts, params = parse_backend_uri(conf.BACKEND) - for host in hosts.split(';'): - try: - caches.append((host, get_cache('%s://%s?%s' % (schema, host, params))._cache)) - except Exception, e: - self.logger.exception(e) + if isinstance(conf.BACKEND, dict) and django.get_version() >= "1.3": + for name in conf.BACKEND.keys(): + backend, location, params = parse_backend_conf(name) + if ';' in location: + location = location.split(';') + try: + mod_path, cls_name = backend.rsplit('.', 1) + mod = importlib.import_module(mod_path) + backend_cls = getattr(mod, cls_name) + for host in location: + caches.append((host, backend_cls(host, params)._cache)) + except Exception, e: + self.logger.exception(e) + elif '://' in conf.BACKEND: + schema, hosts, prams = parse_backend_uri(conf.BACKEND) + for host in hosts.split(';'): + try: + caches.append((host, get_cache('%s://%s?%s' % (schema, host, params))._cache)) + except Exception, e: + self.logger.exception(e) + else: + self.logger.warning('No cache was configured') return caches def get_stats(self, timeout=5):