mirror of
https://github.com/django/django.git
synced 2025-01-10 18:36:05 +00:00
Merge pull request #218 from mgrouchy/ticket_18582
Fixed #18582 -- Added a no-op close to BaseCache
This commit is contained in:
commit
4c5cea7073
1
AUTHORS
1
AUTHORS
@ -231,6 +231,7 @@ answer newbie questions, and generally made Django that much better:
|
|||||||
Simon Greenhill <dev@simon.net.nz>
|
Simon Greenhill <dev@simon.net.nz>
|
||||||
Owen Griffiths
|
Owen Griffiths
|
||||||
Espen Grindhaug <http://grindhaug.org/>
|
Espen Grindhaug <http://grindhaug.org/>
|
||||||
|
Mike Grouchy <http://mikegrouchy.com/>
|
||||||
Janos Guljas
|
Janos Guljas
|
||||||
Thomas Güttler <hv@tbz-pariv.de>
|
Thomas Güttler <hv@tbz-pariv.de>
|
||||||
Horst Gutmann <zerok@zerokspot.com>
|
Horst Gutmann <zerok@zerokspot.com>
|
||||||
|
6
django/core/cache/__init__.py
vendored
6
django/core/cache/__init__.py
vendored
@ -134,11 +134,9 @@ def get_cache(backend, **kwargs):
|
|||||||
"Could not find backend '%s': %s" % (backend, e))
|
"Could not find backend '%s': %s" % (backend, e))
|
||||||
cache = backend_cls(location, params)
|
cache = backend_cls(location, params)
|
||||||
# Some caches -- python-memcached in particular -- need to do a cleanup at the
|
# 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
|
# end of a request cycle. If not implemented in a particular backend
|
||||||
# here.
|
# cache.close is a no-op
|
||||||
if hasattr(cache, 'close'):
|
|
||||||
signals.request_finished.connect(cache.close)
|
signals.request_finished.connect(cache.close)
|
||||||
return cache
|
return cache
|
||||||
|
|
||||||
cache = get_cache(DEFAULT_CACHE_ALIAS)
|
cache = get_cache(DEFAULT_CACHE_ALIAS)
|
||||||
|
|
||||||
|
9
django/core/cache/backends/base.py
vendored
9
django/core/cache/backends/base.py
vendored
@ -6,15 +6,18 @@ import warnings
|
|||||||
from django.core.exceptions import ImproperlyConfigured, DjangoRuntimeWarning
|
from django.core.exceptions import ImproperlyConfigured, DjangoRuntimeWarning
|
||||||
from django.utils.importlib import import_module
|
from django.utils.importlib import import_module
|
||||||
|
|
||||||
|
|
||||||
class InvalidCacheBackendError(ImproperlyConfigured):
|
class InvalidCacheBackendError(ImproperlyConfigured):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class CacheKeyWarning(DjangoRuntimeWarning):
|
class CacheKeyWarning(DjangoRuntimeWarning):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Memcached does not accept keys longer than this.
|
# Memcached does not accept keys longer than this.
|
||||||
MEMCACHE_MAX_KEY_LENGTH = 250
|
MEMCACHE_MAX_KEY_LENGTH = 250
|
||||||
|
|
||||||
|
|
||||||
def default_key_func(key, key_prefix, version):
|
def default_key_func(key, key_prefix, version):
|
||||||
"""
|
"""
|
||||||
Default function to generate keys.
|
Default function to generate keys.
|
||||||
@ -25,6 +28,7 @@ def default_key_func(key, key_prefix, version):
|
|||||||
"""
|
"""
|
||||||
return ':'.join([key_prefix, str(version), key])
|
return ':'.join([key_prefix, str(version), key])
|
||||||
|
|
||||||
|
|
||||||
def get_key_func(key_func):
|
def get_key_func(key_func):
|
||||||
"""
|
"""
|
||||||
Function to decide which key function to use.
|
Function to decide which key function to use.
|
||||||
@ -40,6 +44,7 @@ def get_key_func(key_func):
|
|||||||
return getattr(key_func_module, key_func_name)
|
return getattr(key_func_module, key_func_name)
|
||||||
return default_key_func
|
return default_key_func
|
||||||
|
|
||||||
|
|
||||||
class BaseCache(object):
|
class BaseCache(object):
|
||||||
def __init__(self, params):
|
def __init__(self, params):
|
||||||
timeout = params.get('timeout', params.get('TIMEOUT', 300))
|
timeout = params.get('timeout', params.get('TIMEOUT', 300))
|
||||||
@ -221,3 +226,7 @@ class BaseCache(object):
|
|||||||
the new version.
|
the new version.
|
||||||
"""
|
"""
|
||||||
return self.incr_version(key, -delta, version)
|
return self.incr_version(key, -delta, version)
|
||||||
|
|
||||||
|
def close(self, **kwargs):
|
||||||
|
"""Close the cache connection"""
|
||||||
|
pass
|
||||||
|
@ -785,6 +785,16 @@ nonexistent cache key.::
|
|||||||
However, if the backend doesn't natively provide an increment/decrement
|
However, if the backend doesn't natively provide an increment/decrement
|
||||||
operation, it will be implemented using a two-step retrieve/update.
|
operation, it will be implemented using a two-step retrieve/update.
|
||||||
|
|
||||||
|
|
||||||
|
You can close the connection to your cache with ``close()`` if implemented by
|
||||||
|
the cache backend.
|
||||||
|
|
||||||
|
>>> cache.close()
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
For caches that don't implement ``close`` methods it is a no-op.
|
||||||
|
|
||||||
.. _cache_key_prefixing:
|
.. _cache_key_prefixing:
|
||||||
|
|
||||||
Cache key prefixing
|
Cache key prefixing
|
||||||
|
4
tests/regressiontests/cache/tests.py
vendored
4
tests/regressiontests/cache/tests.py
vendored
@ -270,6 +270,10 @@ class BaseCacheTests(object):
|
|||||||
self.assertEqual(self.cache.decr('answer', -10), 42)
|
self.assertEqual(self.cache.decr('answer', -10), 42)
|
||||||
self.assertRaises(ValueError, self.cache.decr, 'does_not_exist')
|
self.assertRaises(ValueError, self.cache.decr, 'does_not_exist')
|
||||||
|
|
||||||
|
def test_close(self):
|
||||||
|
self.assertTrue(hasattr(self.cache, 'close'))
|
||||||
|
self.cache.close()
|
||||||
|
|
||||||
def test_data_types(self):
|
def test_data_types(self):
|
||||||
# Many different data types can be cached
|
# Many different data types can be cached
|
||||||
stuff = {
|
stuff = {
|
||||||
|
Loading…
Reference in New Issue
Block a user