app/django/core/cache/__init__.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
--- a/app/django/core/cache/__init__.py	Tue Oct 14 12:36:55 2008 +0000
+++ b/app/django/core/cache/__init__.py	Tue Oct 14 16:00:59 2008 +0000
@@ -17,10 +17,13 @@
 
 from cgi import parse_qsl
 from django.conf import settings
+from django.core import signals
 from django.core.cache.backends.base import InvalidCacheBackendError
 
+# Name for use in settings file --> name of module in "backends" directory.
+# Any backend scheme that is not in this dictionary is treated as a Python
+# import path to a custom backend.
 BACKENDS = {
-    # name for use in settings file --> name of module in "backends" directory
     'memcached': 'memcached',
     'locmem': 'locmem',
     'file': 'filebased',
@@ -28,24 +31,12 @@
     'dummy': 'dummy',
 }
 
-DEPRECATED_BACKENDS = {
-    # deprecated backend --> replacement module
-    'simple': 'locmem',
-}
-
 def get_cache(backend_uri):
     if backend_uri.find(':') == -1:
         raise InvalidCacheBackendError, "Backend URI must start with scheme://"
     scheme, rest = backend_uri.split(':', 1)
     if not rest.startswith('//'):
         raise InvalidCacheBackendError, "Backend URI must start with scheme://"
-    if scheme in DEPRECATED_BACKENDS:
-        import warnings
-        warnings.warn("'%s' backend is deprecated. Use '%s' instead." % 
-            (scheme, DEPRECATED_BACKENDS[scheme]), DeprecationWarning)
-        scheme = DEPRECATED_BACKENDS[scheme]
-    if scheme not in BACKENDS:
-        raise InvalidCacheBackendError, "%r is not a valid cache backend" % scheme
 
     host = rest[2:]
     qpos = rest.find('?')
@@ -57,7 +48,17 @@
     if host.endswith('/'):
         host = host[:-1]
 
-    cache_class = getattr(__import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']), 'CacheClass')
-    return cache_class(host, params)
+    if scheme in BACKENDS:
+        module = __import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, [''])
+    else:
+        module = __import__(scheme, {}, {}, [''])
+    return getattr(module, 'CacheClass')(host, params)
 
 cache = get_cache(settings.CACHE_BACKEND)
+
+# Some caches -- pythont-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, 'close'):
+    signals.request_finished.connect(cache.close)
+