15 See docs/cache.txt for information on the public API. |
15 See docs/cache.txt for information on the public API. |
16 """ |
16 """ |
17 |
17 |
18 from cgi import parse_qsl |
18 from cgi import parse_qsl |
19 from django.conf import settings |
19 from django.conf import settings |
|
20 from django.core import signals |
20 from django.core.cache.backends.base import InvalidCacheBackendError |
21 from django.core.cache.backends.base import InvalidCacheBackendError |
21 |
22 |
|
23 # Name for use in settings file --> name of module in "backends" directory. |
|
24 # Any backend scheme that is not in this dictionary is treated as a Python |
|
25 # import path to a custom backend. |
22 BACKENDS = { |
26 BACKENDS = { |
23 # name for use in settings file --> name of module in "backends" directory |
|
24 'memcached': 'memcached', |
27 'memcached': 'memcached', |
25 'locmem': 'locmem', |
28 'locmem': 'locmem', |
26 'file': 'filebased', |
29 'file': 'filebased', |
27 'db': 'db', |
30 'db': 'db', |
28 'dummy': 'dummy', |
31 'dummy': 'dummy', |
29 } |
|
30 |
|
31 DEPRECATED_BACKENDS = { |
|
32 # deprecated backend --> replacement module |
|
33 'simple': 'locmem', |
|
34 } |
32 } |
35 |
33 |
36 def get_cache(backend_uri): |
34 def get_cache(backend_uri): |
37 if backend_uri.find(':') == -1: |
35 if backend_uri.find(':') == -1: |
38 raise InvalidCacheBackendError, "Backend URI must start with scheme://" |
36 raise InvalidCacheBackendError, "Backend URI must start with scheme://" |
39 scheme, rest = backend_uri.split(':', 1) |
37 scheme, rest = backend_uri.split(':', 1) |
40 if not rest.startswith('//'): |
38 if not rest.startswith('//'): |
41 raise InvalidCacheBackendError, "Backend URI must start with scheme://" |
39 raise InvalidCacheBackendError, "Backend URI must start with scheme://" |
42 if scheme in DEPRECATED_BACKENDS: |
|
43 import warnings |
|
44 warnings.warn("'%s' backend is deprecated. Use '%s' instead." % |
|
45 (scheme, DEPRECATED_BACKENDS[scheme]), DeprecationWarning) |
|
46 scheme = DEPRECATED_BACKENDS[scheme] |
|
47 if scheme not in BACKENDS: |
|
48 raise InvalidCacheBackendError, "%r is not a valid cache backend" % scheme |
|
49 |
40 |
50 host = rest[2:] |
41 host = rest[2:] |
51 qpos = rest.find('?') |
42 qpos = rest.find('?') |
52 if qpos != -1: |
43 if qpos != -1: |
53 params = dict(parse_qsl(rest[qpos+1:])) |
44 params = dict(parse_qsl(rest[qpos+1:])) |
55 else: |
46 else: |
56 params = {} |
47 params = {} |
57 if host.endswith('/'): |
48 if host.endswith('/'): |
58 host = host[:-1] |
49 host = host[:-1] |
59 |
50 |
60 cache_class = getattr(__import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']), 'CacheClass') |
51 if scheme in BACKENDS: |
61 return cache_class(host, params) |
52 module = __import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']) |
|
53 else: |
|
54 module = __import__(scheme, {}, {}, ['']) |
|
55 return getattr(module, 'CacheClass')(host, params) |
62 |
56 |
63 cache = get_cache(settings.CACHE_BACKEND) |
57 cache = get_cache(settings.CACHE_BACKEND) |
|
58 |
|
59 # Some caches -- pythont-memcached in particular -- need to do a cleanup at the |
|
60 # end of a request cycle. If the cache provides a close() method, wire it up |
|
61 # here. |
|
62 if hasattr(cache, 'close'): |
|
63 signals.request_finished.connect(cache.close) |
|
64 |