summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2012-11-11 07:18:45 -0800
committerAymeric Augustin <aymeric.augustin@m4x.org>2012-11-11 07:18:45 -0800
commit4c5cea70734fe8f4082c6a9bd8b26cf0a157ca78 (patch)
treede4a70a9ae157d433d759c51e9e39273658d10b5
parentcc0ac26f4a3947be8a3fc55d4784d3474b640c23 (diff)
parenta2e927b7ed25bd319f866c55b18cd214a149d859 (diff)
Merge pull request #218 from mgrouchy/ticket_18582
Fixed #18582 -- Added a no-op close to BaseCache
-rw-r--r--AUTHORS1
-rw-r--r--django/core/cache/__init__.py8
-rw-r--r--django/core/cache/backends/base.py9
-rw-r--r--docs/topics/cache.txt10
-rw-r--r--tests/regressiontests/cache/tests.py4
5 files changed, 27 insertions, 5 deletions
diff --git a/AUTHORS b/AUTHORS
index 6f9b410cca..7a04f1f243 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -231,6 +231,7 @@ answer newbie questions, and generally made Django that much better:
Simon Greenhill <dev@simon.net.nz>
Owen Griffiths
Espen Grindhaug <http://grindhaug.org/>
+ Mike Grouchy <http://mikegrouchy.com/>
Janos Guljas
Thomas Güttler <hv@tbz-pariv.de>
Horst Gutmann <zerok@zerokspot.com>
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py
index d1a02a9f13..562bcc21bb 100644
--- a/django/core/cache/__init__.py
+++ b/django/core/cache/__init__.py
@@ -134,11 +134,9 @@ def get_cache(backend, **kwargs):
"Could not find backend '%s': %s" % (backend, e))
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)
+ # end of a request cycle. If not implemented in a particular backend
+ # cache.close is a no-op
+ signals.request_finished.connect(cache.close)
return cache
cache = get_cache(DEFAULT_CACHE_ALIAS)
-
diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
index 06e8952bfb..25fbbcbbdd 100644
--- a/django/core/cache/backends/base.py
+++ b/django/core/cache/backends/base.py
@@ -6,15 +6,18 @@ import warnings
from django.core.exceptions import ImproperlyConfigured, DjangoRuntimeWarning
from django.utils.importlib import import_module
+
class InvalidCacheBackendError(ImproperlyConfigured):
pass
+
class CacheKeyWarning(DjangoRuntimeWarning):
pass
# Memcached does not accept keys longer than this.
MEMCACHE_MAX_KEY_LENGTH = 250
+
def default_key_func(key, key_prefix, version):
"""
Default function to generate keys.
@@ -25,6 +28,7 @@ def default_key_func(key, key_prefix, version):
"""
return ':'.join([key_prefix, str(version), key])
+
def get_key_func(key_func):
"""
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 default_key_func
+
class BaseCache(object):
def __init__(self, params):
timeout = params.get('timeout', params.get('TIMEOUT', 300))
@@ -221,3 +226,7 @@ class BaseCache(object):
the new version.
"""
return self.incr_version(key, -delta, version)
+
+ def close(self, **kwargs):
+ """Close the cache connection"""
+ pass
diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt
index 2f95c33dd5..a15cf58370 100644
--- a/docs/topics/cache.txt
+++ b/docs/topics/cache.txt
@@ -785,6 +785,16 @@ nonexistent cache key.::
However, if the backend doesn't natively provide an increment/decrement
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
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
index 9960c01300..fc92ecf870 100644
--- a/tests/regressiontests/cache/tests.py
+++ b/tests/regressiontests/cache/tests.py
@@ -270,6 +270,10 @@ class BaseCacheTests(object):
self.assertEqual(self.cache.decr('answer', -10), 42)
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):
# Many different data types can be cached
stuff = {