Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7398 -- Allow for custom cache-backends to be used.

Based on a patch from Lau Bech Lauritzen and Brenton Simpson.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8075 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7c33cc7f5ef4597b55dde561c1752db4d0feaca1 1 parent 7c0cf54
Malcolm Tredinnick authored
2  AUTHORS
@@ -236,6 +236,7 @@ answer newbie questions, and generally made Django that much better:
236 236
     Stuart Langridge <http://www.kryogenix.org/>
237 237
     Paul Lanier <planier@google.com>
238 238
     Nicola Larosa <nico@teknico.net>
  239
+    Lau Bech Lauritzen
239 240
     Rune Rønde Laursen <runerl@skjoldhoej.dk>
240 241
     Eugene Lazutkin <http://lazutkin.com/blog/>
241 242
     lcordier@point45.com
@@ -341,6 +342,7 @@ answer newbie questions, and generally made Django that much better:
341 342
     Pete Shinners <pete@shinners.org>
342 343
     Leo Shklovskii
343 344
     jason.sidabras@gmail.com
  345
+    Brenton Simpson <http://theillustratedlife.com>
344 346
     Jozko Skrablin <jozko.skrablin@gmail.com>
345 347
     Ben Slavin <benjamin.slavin@gmail.com>
346 348
     sloonz <simon.lipp@insa-lyon.fr>
13  django/core/cache/__init__.py
@@ -19,8 +19,10 @@
19 19
 from django.conf import settings
20 20
 from django.core.cache.backends.base import InvalidCacheBackendError
21 21
 
  22
+# Name for use in settings file --> name of module in "backends" directory.
  23
+# Any backend scheme that is not in this dictionary is treated as a Python
  24
+# import path to a custom backend.
22 25
 BACKENDS = {
23  
-    # name for use in settings file --> name of module in "backends" directory
24 26
     'memcached': 'memcached',
25 27
     'locmem': 'locmem',
26 28
     'file': 'filebased',
@@ -44,8 +46,6 @@ def get_cache(backend_uri):
44 46
         warnings.warn("'%s' backend is deprecated. Use '%s' instead." % 
45 47
             (scheme, DEPRECATED_BACKENDS[scheme]), DeprecationWarning)
46 48
         scheme = DEPRECATED_BACKENDS[scheme]
47  
-    if scheme not in BACKENDS:
48  
-        raise InvalidCacheBackendError, "%r is not a valid cache backend" % scheme
49 49
 
50 50
     host = rest[2:]
51 51
     qpos = rest.find('?')
@@ -57,7 +57,10 @@ def get_cache(backend_uri):
57 57
     if host.endswith('/'):
58 58
         host = host[:-1]
59 59
 
60  
-    cache_class = getattr(__import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']), 'CacheClass')
61  
-    return cache_class(host, params)
  60
+    if scheme in BACKENDS:
  61
+        module = __import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, [''])
  62
+    else:
  63
+        module = __import__(scheme, {}, {}, [''])
  64
+    return getattr(module, 'CacheClass')(host, params)
62 65
 
63 66
 cache = get_cache(settings.CACHE_BACKEND)
25  docs/cache.txt
@@ -6,8 +6,8 @@ A fundamental tradeoff in dynamic Web sites is, well, they're dynamic. Each
6 6
 time a user requests a page, the Web server makes all sorts of calculations --
7 7
 from database queries to template rendering to business logic -- to create the
8 8
 page that your site's visitor sees. This is a lot more expensive, from a
9  
-processing-overhead perspective, than your standard read-a-file-off-the-filesystem
10  
-server arrangement.
  9
+processing-overhead perspective, than your standard
  10
+read-a-file-off-the-filesystem server arrangement.
11 11
 
12 12
 For most Web applications, this overhead isn't a big deal. Most Web
13 13
 applications aren't washingtonpost.com or slashdot.org; they're simply small-
@@ -186,6 +186,27 @@ production environment still will. To activate dummy caching, set
186 186
 
187 187
     CACHE_BACKEND = 'dummy:///'
188 188
 
  189
+Using a custom cache backend
  190
+----------------------------
  191
+
  192
+**New in Django development version**
  193
+
  194
+While Django includes support for a number of cache backends out-of-the-box,
  195
+sometimes you will want to use a customised verison or your own backend.  To
  196
+use an external cache backend with Django, use a Python import path as the
  197
+scheme portion (the part before the initial colon) of the ``CACHE_BACKEND``
  198
+URI, like so::
  199
+
  200
+    CACHE_BACKEND = 'path.to.backend://'
  201
+
  202
+If you're building your own backend, you can use the standard cache backends
  203
+as reference implementations. You'll find the code in the
  204
+``django/core/cache/backends/`` directory of the Django source.
  205
+
  206
+Note: Without a really compelling reason, like a host that doesn't support the
  207
+them, you should stick to the cache backends included with Django. They've
  208
+been really well-tested and are quite easy to use.
  209
+
189 210
 CACHE_BACKEND arguments
190 211
 -----------------------
191 212
 

0 notes on commit 7c33cc7

Please sign in to comment.
Something went wrong with that request. Please try again.