Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

BaseCache now has a no-op close method as per ticket #18582 #218

Merged
merged 1 commit into from over 1 year ago

3 participants

Mike Grouchy Claude Paroz Aymeric Augustin
Mike Grouchy

Also removed the hasattr check when firing request_finished signal for
caches with a 'close' method. Should be safe to call cache.close
everywhere now

references ticket: https://code.djangoproject.com/ticket/18482

Claude Paroz claudep commented on the diff July 18, 2012
tests/regressiontests/cache/tests.py
... ...
@@ -266,6 +266,10 @@ def test_decr(self):
266 266
         self.assertEqual(self.cache.get('answer'), 32)
267 267
         self.assertRaises(ValueError, self.cache.decr, 'does_not_exist')
268 268
 
  269
+    def test_close(self):
  270
+        self.assertEqual(hasattr(self.cache, 'close'), True)
  271
+        self.cache.close()
  272
+
269 273
     def test_data_types(self):
270 274
         # Many different data types can be cached
271 275
         stuff = {
2
Claude Paroz Owner
claudep added a note July 18, 2012

assertEqual(..., True) -> assertTrue(...)

Mike Grouchy
mgrouchy added a note July 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mike Grouchy BaseCache now has a no-op close method as per ticket #18582
Also removed the hasattr check when firing request_finished signal for
caches with a 'close' method. Should be safe to call `cache.close`
everywhere now
a2e927b
Mike Grouchy

updated as per feedback from @claudep.

Mike Grouchy

Is there anything I can do to get this merged? Ticket says this is ready for checkin?

Mike Grouchy

Anything I can do to help this along to get it merged in before the code freeze for 1.5. No big deal, just trying to get this closed out.

Aymeric Augustin aaugustin merged commit 4c5cea7 into from November 11, 2012
Aymeric Augustin aaugustin closed this November 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jul 18, 2012
Mike Grouchy BaseCache now has a no-op close method as per ticket #18582
Also removed the hasattr check when firing request_finished signal for
caches with a 'close' method. Should be safe to call `cache.close`
everywhere now
a2e927b
This page is out of date. Refresh to see the latest.
1  AUTHORS
@@ -229,6 +229,7 @@ answer newbie questions, and generally made Django that much better:
229 229
     Simon Greenhill <dev@simon.net.nz>
230 230
     Owen Griffiths
231 231
     Espen Grindhaug <http://grindhaug.org/>
  232
+    Mike Grouchy <http://mikegrouchy.com/>
232 233
     Janos Guljas
233 234
     Thomas Güttler <hv@tbz-pariv.de>
234 235
     Horst Gutmann <zerok@zerokspot.com>
8  django/core/cache/__init__.py
@@ -133,11 +133,9 @@ def get_cache(backend, **kwargs):
133 133
             "Could not find backend '%s': %s" % (backend, e))
134 134
     cache = backend_cls(location, params)
135 135
     # Some caches -- python-memcached in particular -- need to do a cleanup at the
136  
-    # end of a request cycle. If the cache provides a close() method, wire it up
137  
-    # here.
138  
-    if hasattr(cache, 'close'):
139  
-        signals.request_finished.connect(cache.close)
  136
+    # end of a request cycle. If not implemented in a particular backend
  137
+    # cache.close is a no-op
  138
+    signals.request_finished.connect(cache.close)
140 139
     return cache
141 140
 
142 141
 cache = get_cache(DEFAULT_CACHE_ALIAS)
143  
-
9  django/core/cache/backends/base.py
@@ -6,15 +6,18 @@
6 6
 from django.utils.encoding import smart_str
7 7
 from django.utils.importlib import import_module
8 8
 
  9
+
9 10
 class InvalidCacheBackendError(ImproperlyConfigured):
10 11
     pass
11 12
 
  13
+
12 14
 class CacheKeyWarning(DjangoRuntimeWarning):
13 15
     pass
14 16
 
15 17
 # Memcached does not accept keys longer than this.
16 18
 MEMCACHE_MAX_KEY_LENGTH = 250
17 19
 
  20
+
18 21
 def default_key_func(key, key_prefix, version):
19 22
     """
20 23
     Default function to generate keys.
@@ -25,6 +28,7 @@ def default_key_func(key, key_prefix, version):
25 28
     """
26 29
     return ':'.join([key_prefix, str(version), smart_str(key)])
27 30
 
  31
+
28 32
 def get_key_func(key_func):
29 33
     """
30 34
     Function to decide which key function to use.
@@ -40,6 +44,7 @@ def get_key_func(key_func):
40 44
             return getattr(key_func_module, key_func_name)
41 45
     return default_key_func
42 46
 
  47
+
43 48
 class BaseCache(object):
44 49
     def __init__(self, params):
45 50
         timeout = params.get('timeout', params.get('TIMEOUT', 300))
@@ -221,3 +226,7 @@ def decr_version(self, key, delta=1, version=None):
221 226
         the new version.
222 227
         """
223 228
         return self.incr_version(key, -delta, version)
  229
+
  230
+    def close(self, **kwargs):
  231
+        """Close the cache connection"""
  232
+        pass
10  docs/topics/cache.txt
@@ -779,6 +779,16 @@ nonexistent cache key.::
779 779
     However, if the backend doesn't natively provide an increment/decrement
780 780
     operation, it will be implemented using a two-step retrieve/update.
781 781
 
  782
+
  783
+You can close the connection to your cache with ``close()`` if implemented by
  784
+the cache backend.
  785
+
  786
+    >>> cache.close()
  787
+
  788
+.. note::
  789
+
  790
+    For caches that don't implement ``close`` methods it is a no-op.
  791
+
782 792
 .. _cache_key_prefixing:
783 793
 
784 794
 Cache key prefixing
4  tests/regressiontests/cache/tests.py
@@ -266,6 +266,10 @@ def test_decr(self):
266 266
         self.assertEqual(self.cache.get('answer'), 32)
267 267
         self.assertRaises(ValueError, self.cache.decr, 'does_not_exist')
268 268
 
  269
+    def test_close(self):
  270
+        self.assertTrue(hasattr(self.cache, 'close'))
  271
+        self.cache.close()
  272
+
269 273
     def test_data_types(self):
270 274
         # Many different data types can be cached
271 275
         stuff = {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.