Skip to content

Commit

Permalink
Fixed #17286 -- Made sure all cache backends are set up to connect to…
Browse files Browse the repository at this point in the history
… 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
jezdez committed Feb 9, 2012
1 parent 75c60e8 commit c609b79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 7 additions & 6 deletions django/core/cache/__init__.py
Expand Up @@ -176,12 +176,13 @@ def get_cache(backend, **kwargs):
except (AttributeError, ImportError), e: except (AttributeError, ImportError), e:
raise InvalidCacheBackendError( raise InvalidCacheBackendError(
"Could not find backend '%s': %s" % (backend, e)) "Could not find backend '%s': %s" % (backend, e))
return backend_cls(location, params) cache = backend_cls(location, params)
# 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, 'close'):
signals.request_finished.connect(cache.close)
return cache


cache = get_cache(DEFAULT_CACHE_ALIAS) cache = get_cache(DEFAULT_CACHE_ALIAS)


# 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, 'close'):
signals.request_finished.connect(cache.close)
7 changes: 7 additions & 0 deletions tests/regressiontests/cache/tests.py
Expand Up @@ -1040,6 +1040,13 @@ def test_simple(self):


self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist') self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist')


def test_close(self):
from django.core import signals
cache = get_cache('regressiontests.cache.closeable_cache.CacheClass')
self.assertFalse(cache.closed)
signals.request_finished.send(self.__class__)
self.assertTrue(cache.closed)



class CacheUtils(TestCase): class CacheUtils(TestCase):
"""TestCase for django.utils.cache functions.""" """TestCase for django.utils.cache functions."""
Expand Down

0 comments on commit c609b79

Please sign in to comment.