Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17286 -- Made sure all cache backends are set up to connect to…

… the signal handler that closes the cache connection when the request has been processed. Thanks, gnosek.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17479 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c609b792f74948bc4e485ebfcb456ef1b6f8ce0a 1 parent 75c60e8
Jannis Leidel authored February 09, 2012
13  django/core/cache/__init__.py
@@ -176,12 +176,13 @@ def get_cache(backend, **kwargs):
176 176
     except (AttributeError, ImportError), e:
177 177
         raise InvalidCacheBackendError(
178 178
             "Could not find backend '%s': %s" % (backend, e))
179  
-    return backend_cls(location, params)
  179
+    cache = backend_cls(location, params)
  180
+    # Some caches -- python-memcached in particular -- need to do a cleanup at the
  181
+    # end of a request cycle. If the cache provides a close() method, wire it up
  182
+    # here.
  183
+    if hasattr(cache, 'close'):
  184
+        signals.request_finished.connect(cache.close)
  185
+    return cache
180 186
 
181 187
 cache = get_cache(DEFAULT_CACHE_ALIAS)
182 188
 
183  
-# Some caches -- python-memcached in particular -- need to do a cleanup at the
184  
-# end of a request cycle. If the cache provides a close() method, wire it up
185  
-# here.
186  
-if hasattr(cache, 'close'):
187  
-    signals.request_finished.connect(cache.close)
7  tests/regressiontests/cache/tests.py
@@ -1040,6 +1040,13 @@ def test_simple(self):
1040 1040
 
1041 1041
         self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist')
1042 1042
 
  1043
+    def test_close(self):
  1044
+        from django.core import signals
  1045
+        cache = get_cache('regressiontests.cache.closeable_cache.CacheClass')
  1046
+        self.assertFalse(cache.closed)
  1047
+        signals.request_finished.send(self.__class__)
  1048
+        self.assertTrue(cache.closed)
  1049
+
1043 1050
 
1044 1051
 class CacheUtils(TestCase):
1045 1052
     """TestCase for django.utils.cache functions."""

0 notes on commit c609b79

Please sign in to comment.
Something went wrong with that request. Please try again.