diff options
| author | Curtis Maloney <curtis@tinbrain.net> | 2013-09-19 18:38:56 +1000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-09-19 10:01:39 -0400 |
| commit | 4e9f800742c3048402acbaea67ec3c6bc3bd0935 (patch) | |
| tree | 35620b801806d70870e6371ab4b78c7fc2d6627b /django | |
| parent | 7fec5a2240835af7c7f3accd64d9d894d4f92782 (diff) | |
Fixed #21125 -- Removed support for cache URI syntax
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/cache/__init__.py | 94 |
1 files changed, 20 insertions, 74 deletions
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py index 4cafeccd2a..c242671c6c 100644 --- a/django/core/cache/__init__.py +++ b/django/core/cache/__init__.py @@ -8,9 +8,9 @@ the abstract BaseCache class in django.core.cache.backends.base. Client code should not access a cache backend directly; instead it should either use the "cache" variable made available here, or it should use the -get_cache() function made available here. get_cache() takes a backend URI -(e.g. "memcached://127.0.0.1:11211/") and returns an instance of a backend -cache class. +get_cache() function made available here. get_cache() takes a CACHES alias or a +backend path and config parameters, and returns an instance of a backend cache +class. See docs/topics/cache.txt for information on the public API. """ @@ -29,78 +29,17 @@ __all__ = [ 'get_cache', 'cache', 'DEFAULT_CACHE_ALIAS' ] -# 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 = { - 'memcached': 'memcached', - 'locmem': 'locmem', - 'file': 'filebased', - 'db': 'db', - 'dummy': 'dummy', -} - DEFAULT_CACHE_ALIAS = 'default' -def parse_backend_uri(backend_uri): - """ - Converts the "backend_uri" into a cache scheme ('db', 'memcached', etc), a - host and any extra params that are required for the backend. Returns a - (scheme, host, params) tuple. - """ - if backend_uri.find(':') == -1: - raise InvalidCacheBackendError("Backend URI must start with scheme://") - scheme, rest = backend_uri.split(':', 1) - if not rest.startswith('//'): - raise InvalidCacheBackendError("Backend URI must start with scheme://") - - host = rest[2:] - qpos = rest.find('?') - if qpos != -1: - params = dict(parse_qsl(rest[qpos+1:])) - host = rest[2:qpos] - else: - params = {} - if host.endswith('/'): - host = host[:-1] - - return scheme, host, params - if DEFAULT_CACHE_ALIAS not in settings.CACHES: raise ImproperlyConfigured("You must define a '%s' cache" % DEFAULT_CACHE_ALIAS) -def parse_backend_conf(backend, **kwargs): - """ - Helper function to parse the backend configuration - that doesn't use the URI notation. - """ - # Try to get the CACHES entry for the given backend name first - conf = settings.CACHES.get(backend, None) - if conf is not None: - args = conf.copy() - args.update(kwargs) - backend = args.pop('BACKEND') - location = args.pop('LOCATION', '') - return backend, location, args - else: - try: - # Trying to import the given backend, in case it's a dotted path - import_by_path(backend) - except ImproperlyConfigured as e: - raise InvalidCacheBackendError("Could not find backend '%s': %s" % ( - backend, e)) - location = kwargs.pop('LOCATION', '') - return backend, location, kwargs def get_cache(backend, **kwargs): """ Function to load a cache backend dynamically. This is flexible by design to allow different use cases: - To load a backend with the old URI-based notation:: - - cache = get_cache('locmem://') - To load a backend that is pre-defined in the settings:: cache = get_cache('default') @@ -114,17 +53,24 @@ def get_cache(backend, **kwargs): """ try: - if '://' in backend: - # for backwards compatibility - backend, location, params = parse_backend_uri(backend) - if backend in BACKENDS: - backend = 'django.core.cache.backends.%s' % BACKENDS[backend] - params.update(kwargs) - mod = importlib.import_module(backend) - backend_cls = mod.CacheClass + # Try to get the CACHES entry for the given backend name first + try: + conf = settings.CACHES[backend] + except KeyError: + try: + # Trying to import the given backend, in case it's a dotted path + import_by_path(backend) + except ImproperlyConfigured as e: + raise InvalidCacheBackendError("Could not find backend '%s': %s" % ( + backend, e)) + location = kwargs.pop('LOCATION', '') + params = kwargs else: - backend, location, params = parse_backend_conf(backend, **kwargs) - backend_cls = import_by_path(backend) + params = conf.copy() + params.update(kwargs) + backend = params.pop('BACKEND') + location = params.pop('LOCATION', '') + backend_cls = import_by_path(backend) except (AttributeError, ImportError, ImproperlyConfigured) as e: raise InvalidCacheBackendError( "Could not find backend '%s': %s" % (backend, e)) |
