Ticket #17286: 17286.diff

File 17286.diff, 2.3 KB (added by Grzegorz Nosek, 12 years ago)
  • django/core/cache/__init__.py

    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):  
    176176    except (AttributeError, ImportError), e:
    177177        raise InvalidCacheBackendError(
    178178            "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
    180186
    181187cache = get_cache(DEFAULT_CACHE_ALIAS)
    182188
    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)
  • new file tests/regressiontests/cache/closeable_cache.py

    diff --git a/tests/regressiontests/cache/closeable_cache.py b/tests/regressiontests/cache/closeable_cache.py
    new file mode 100644
    index 0000000..8307385
    - +  
     1from django.core.cache.backends.locmem import LocMemCache
     2
     3
     4class CloseHookMixin(object):
     5    closed = False
     6
     7    def close(self, **kwargs):
     8        self.closed = True
     9
     10class CacheClass(CloseHookMixin, LocMemCache):
     11    pass
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    index 0e28f66..bd29cde 100644
    a b class GetCacheTests(unittest.TestCase):  
    10401040
    10411041        self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist')
    10421042
     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
    10431050
    10441051class CacheUtils(TestCase):
    10451052    """TestCase for django.utils.cache functions."""
Back to Top