1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

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
This commit is contained in:
Malcolm Tredinnick 2008-07-25 18:51:32 +00:00
parent 7c0cf545ef
commit 7c33cc7f5e
3 changed files with 33 additions and 7 deletions

View File

@ -236,6 +236,7 @@ answer newbie questions, and generally made Django that much better:
Stuart Langridge <http://www.kryogenix.org/> Stuart Langridge <http://www.kryogenix.org/>
Paul Lanier <planier@google.com> Paul Lanier <planier@google.com>
Nicola Larosa <nico@teknico.net> Nicola Larosa <nico@teknico.net>
Lau Bech Lauritzen
Rune Rønde Laursen <runerl@skjoldhoej.dk> Rune Rønde Laursen <runerl@skjoldhoej.dk>
Eugene Lazutkin <http://lazutkin.com/blog/> Eugene Lazutkin <http://lazutkin.com/blog/>
lcordier@point45.com lcordier@point45.com
@ -341,6 +342,7 @@ answer newbie questions, and generally made Django that much better:
Pete Shinners <pete@shinners.org> Pete Shinners <pete@shinners.org>
Leo Shklovskii Leo Shklovskii
jason.sidabras@gmail.com jason.sidabras@gmail.com
Brenton Simpson <http://theillustratedlife.com>
Jozko Skrablin <jozko.skrablin@gmail.com> Jozko Skrablin <jozko.skrablin@gmail.com>
Ben Slavin <benjamin.slavin@gmail.com> Ben Slavin <benjamin.slavin@gmail.com>
sloonz <simon.lipp@insa-lyon.fr> sloonz <simon.lipp@insa-lyon.fr>

View File

@ -19,8 +19,10 @@ from cgi import parse_qsl
from django.conf import settings from django.conf import settings
from django.core.cache.backends.base import InvalidCacheBackendError from django.core.cache.backends.base import InvalidCacheBackendError
# Name for use in settings file --> name of module in "backends" directory.
# Any backend scheme that is not in this dictionary is treated as a Python
# import path to a custom backend.
BACKENDS = { BACKENDS = {
# name for use in settings file --> name of module in "backends" directory
'memcached': 'memcached', 'memcached': 'memcached',
'locmem': 'locmem', 'locmem': 'locmem',
'file': 'filebased', 'file': 'filebased',
@ -44,8 +46,6 @@ def get_cache(backend_uri):
warnings.warn("'%s' backend is deprecated. Use '%s' instead." % warnings.warn("'%s' backend is deprecated. Use '%s' instead." %
(scheme, DEPRECATED_BACKENDS[scheme]), DeprecationWarning) (scheme, DEPRECATED_BACKENDS[scheme]), DeprecationWarning)
scheme = DEPRECATED_BACKENDS[scheme] scheme = DEPRECATED_BACKENDS[scheme]
if scheme not in BACKENDS:
raise InvalidCacheBackendError, "%r is not a valid cache backend" % scheme
host = rest[2:] host = rest[2:]
qpos = rest.find('?') qpos = rest.find('?')
@ -57,7 +57,10 @@ def get_cache(backend_uri):
if host.endswith('/'): if host.endswith('/'):
host = host[:-1] host = host[:-1]
cache_class = getattr(__import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']), 'CacheClass') if scheme in BACKENDS:
return cache_class(host, params) module = __import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, [''])
else:
module = __import__(scheme, {}, {}, [''])
return getattr(module, 'CacheClass')(host, params)
cache = get_cache(settings.CACHE_BACKEND) cache = get_cache(settings.CACHE_BACKEND)

View File

@ -6,8 +6,8 @@ A fundamental tradeoff in dynamic Web sites is, well, they're dynamic. Each
time a user requests a page, the Web server makes all sorts of calculations -- time a user requests a page, the Web server makes all sorts of calculations --
from database queries to template rendering to business logic -- to create the from database queries to template rendering to business logic -- to create the
page that your site's visitor sees. This is a lot more expensive, from a page that your site's visitor sees. This is a lot more expensive, from a
processing-overhead perspective, than your standard read-a-file-off-the-filesystem processing-overhead perspective, than your standard
server arrangement. read-a-file-off-the-filesystem server arrangement.
For most Web applications, this overhead isn't a big deal. Most Web For most Web applications, this overhead isn't a big deal. Most Web
applications aren't washingtonpost.com or slashdot.org; they're simply small- 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
CACHE_BACKEND = 'dummy:///' CACHE_BACKEND = 'dummy:///'
Using a custom cache backend
----------------------------
**New in Django development version**
While Django includes support for a number of cache backends out-of-the-box,
sometimes you will want to use a customised verison or your own backend. To
use an external cache backend with Django, use a Python import path as the
scheme portion (the part before the initial colon) of the ``CACHE_BACKEND``
URI, like so::
CACHE_BACKEND = 'path.to.backend://'
If you're building your own backend, you can use the standard cache backends
as reference implementations. You'll find the code in the
``django/core/cache/backends/`` directory of the Django source.
Note: Without a really compelling reason, like a host that doesn't support the
them, you should stick to the cache backends included with Django. They've
been really well-tested and are quite easy to use.
CACHE_BACKEND arguments CACHE_BACKEND arguments
----------------------- -----------------------