Commits

Jannis Leidel committed a0f9458

Moved settings to own module and added ability to provide a separate cache backend for Johnny, e.g.:

JOHNNY_CACHE_BACKEND = 'johnny.backends.memcached://...'

Comments (0)

Files changed (6)

 except ImportError:
     from md5 import md5
 
-from django.conf import settings
 import localstore
 import signals
+from johnny import settings
 from transaction import TransactionManager
 
 try:
         return False
 
 local = localstore.LocalStore()
-blacklist = getattr(settings, 'MAN_IN_BLACKLIST',
-            getattr(settings, 'JOHNNY_TABLE_BLACKLIST', []))
-blacklist = set(blacklist)
 
 def blacklist_match(*tables):
     """Returns True if a set of tables is in the blacklist, False otherwise."""
     # should have relatively few tables involved, and I don't imagine that
     # blacklists would grow very vast.  The fastest i've been able to come
     # up with is to pre-create a blacklist set and use intersect.
-    return bool(blacklist.intersection(tables))
+    return bool(settings.BLACKLIST.intersection(tables))
 
 def get_backend():
     """Get's a QueryCacheBackend class for the current version of django."""
     __shared_state = {}
     def __init__(self, cache_backend=None, keyhandler=None, keygen=None):
         self.__dict__ = self.__shared_state
-        self.prefix = getattr(settings, 'JOHNNY_MIDDLEWARE_KEY_PREFIX', 'jc')
+        self.prefix = settings.MIDDLEWARE_KEY_PREFIX
         if keyhandler: self.kh_class = keyhandler
         if keygen: self.kg_class = keygen
         if not cache_backend and not hasattr(self, 'cache_backend'):

johnny/middleware.py

 
 import django
 from django.core.exceptions import ImproperlyConfigured
-from django.core.cache import cache as django_cache
+from django.core.cache import get_cache
 from django.middleware import transaction as trans_middleware
 from django.db import transaction
-from johnny import cache
+from johnny import cache, settings
+
+if settings.CACHE_BACKEND:
+    cache_backend = get_cache(settings.CACHE_BACKEND)
+
+    # Some caches -- python-memcached in particular -- need to do a cleanup at the
+    # end of a request cycle. If the cache provides a close() method, wire it up
+    # here.
+    if hasattr(cache_backend, 'close'):
+        signals.request_finished.connect(cache_backend.close)
+else:
+    from django.core.cache import cache as cache_backend
 
 class QueryCacheMiddleware(object):
     """This middleware class monkey-patches django's ORM to maintain
     __state = {} # alex martinelli's borg pattern
     def __init__(self):
         self.__dict__ = self.__state
-        from django.conf import settings
-        self.disabled = getattr(settings, 'DISABLE_QUERYSET_CACHE', False)
+        self.disabled = settings.DISABLE_QUERYSET_CACHE
         self.installed = getattr(self, 'installed', False)
         if not self.installed and not self.disabled:
             # when we install, lets refresh the blacklist, just in case johnny
             # was loaded before the setting exists somehow...
-            blacklist = getattr(settings, 'MAN_IN_BLACKLIST',
-                        getattr(settings, 'JOHNNY_TABLE_BLACKLIST', []))
+            blacklist = settings.BLACKLIST
             cache.blacklist = set(blacklist)
-            self.query_cache_backend = cache.get_backend()(django_cache)
+            self.query_cache_backend = cache.get_backend()(cache_backend)
             self.query_cache_backend.patch()
             self.installed = True
 

johnny/settings.py

+from django.conf import settings
+
+DISABLE_QUERYSET_CACHE = getattr(settings, 'DISABLE_QUERYSET_CACHE', False)
+
+BLACKLIST = getattr(settings, 'MAN_IN_BLACKLIST',
+            getattr(settings, 'JOHNNY_TABLE_BLACKLIST', []))
+BLACKLIST = set(BLACKLIST)
+
+MIDDLEWARE_KEY_PREFIX = getattr(settings, 'JOHNNY_MIDDLEWARE_KEY_PREFIX', 'jc')
+
+MIDDLEWARE_SECONDS = getattr(settings, 'JOHNNY_MIDDLEWARE_SECONDS', 0)
+
+CACHE_BACKEND = getattr(settings, 'JOHNNY_CACHE_BACKEND', None)

johnny/transaction.py

     from django.utils.functional import wraps  # Python 2.3, 2.4 fallback.
 
 
-from django.conf import settings
 import django
 
 
     """
     _patched_var = False
     def __init__(self, cache_backend, keygen):
-        self.timeout = getattr(settings, 'JOHNNY_MIDDLEWARE_SECONDS', 0)
-        self.prefix = getattr(settings, 'JOHNNY_MIDDLEWARE_KEY_PREFIX', 'jc')
+        from johnny import cache, settings
 
-        from johnny import cache
+        self.timeout = settings.MIDDLEWARE_SECONDS
+        self.prefix = settings.MIDDLEWARE_KEY_PREFIX
+
         self.cache_backend = cache_backend
         self.local = cache.local
         self.keygen = keygen(self.prefix)

johnny/urls.py

Empty file removed.

johnny/views.py

Empty file removed.