From eb9fa0a60ca7135029c8522ca8adbc14457499f9 Mon Sep 17 00:00:00 2001 From: Chris Streeter Date: Tue, 19 Jul 2011 09:20:26 -0700 Subject: [PATCH 1/2] Add support for Django 1.3's CACHES config setting. --- nexus_memcache/conf.py | 8 +++++++- nexus_memcache/nexus_modules.py | 33 +++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/nexus_memcache/conf.py b/nexus_memcache/conf.py index 06de16c..89fa7e3 100644 --- a/nexus_memcache/conf.py +++ b/nexus_memcache/conf.py @@ -1,3 +1,9 @@ +import django from django.conf import settings -BACKEND = getattr(settings, 'NEXUS_MEMCACHE_BACKEND', settings.CACHE_BACKEND) + +BACKEND = getattr(settings, 'NEXUS_MEMCACHE_BACKEND', + getattr(settings, 'CACHE_BACKEND', None)) + +if not BACKEND and django.get_version() >= "1.3": + BACKEND = getattr(settings, 'CACHES', BACKEND) diff --git a/nexus_memcache/nexus_modules.py b/nexus_memcache/nexus_modules.py index df10999..b5129c9 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): From a92a6a16f49f79165c88ebc5c57d1fe726aeddaa Mon Sep 17 00:00:00 2001 From: Chris Streeter Date: Tue, 19 Jul 2011 09:38:43 -0700 Subject: [PATCH 2/2] Fix a type error. --- nexus_memcache/nexus_modules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nexus_memcache/nexus_modules.py b/nexus_memcache/nexus_modules.py index b5129c9..2ccc3c0 100644 --- a/nexus_memcache/nexus_modules.py +++ b/nexus_memcache/nexus_modules.py @@ -29,7 +29,7 @@ def get_caches(self): mod = importlib.import_module(mod_path) backend_cls = getattr(mod, cls_name) for host in location: - caches.append(host, backend_cls(host, params)._cache) + caches.append((host, backend_cls(host, params)._cache)) except Exception, e: self.logger.exception(e) elif '://' in conf.BACKEND: