commit 53e27f875cbefb21c8bbf5d48f242f73048fa6c1
Author: Grzegorz Nosek <root@localdomain.pl>
Date: Sun Feb 5 16:07:44 2012 +0100
#17286
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py
index b97c746..346deae 100644
a
|
b
|
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) |
diff --git a/tests/regressiontests/cache/closeable_cache.py b/tests/regressiontests/cache/closeable_cache.py
new file mode 100644
index 0000000..8307385
-
|
+
|
|
| 1 | from django.core.cache.backends.locmem import LocMemCache |
| 2 | |
| 3 | |
| 4 | class CloseHookMixin(object): |
| 5 | closed = False |
| 6 | |
| 7 | def close(self, **kwargs): |
| 8 | self.closed = True |
| 9 | |
| 10 | class CacheClass(CloseHookMixin, LocMemCache): |
| 11 | pass |
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
index 0e28f66..bd29cde 100644
a
|
b
|
class GetCacheTests(unittest.TestCase):
|
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.""" |