app/django/core/cache/__init__.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 """
       
     2 Caching framework.
       
     3 
       
     4 This package defines set of cache backends that all conform to a simple API.
       
     5 In a nutshell, a cache is a set of values -- which can be any object that
       
     6 may be pickled -- identified by string keys.  For the complete API, see
       
     7 the abstract BaseCache class in django.core.cache.backends.base.
       
     8 
       
     9 Client code should not access a cache backend directly; instead it should
       
    10 either use the "cache" variable made available here, or it should use the
       
    11 get_cache() function made available here. get_cache() takes a backend URI
       
    12 (e.g. "memcached://127.0.0.1:11211/") and returns an instance of a backend
       
    13 cache class.
       
    14 
       
    15 See docs/cache.txt for information on the public API.
       
    16 """
       
    17 
       
    18 from cgi import parse_qsl
       
    19 from django.conf import settings
       
    20 from django.core.cache.backends.base import InvalidCacheBackendError
       
    21 
       
    22 BACKENDS = {
       
    23     # name for use in settings file --> name of module in "backends" directory
       
    24     'memcached': 'memcached',
       
    25     'locmem': 'locmem',
       
    26     'file': 'filebased',
       
    27     'db': 'db',
       
    28     'dummy': 'dummy',
       
    29 }
       
    30 
       
    31 DEPRECATED_BACKENDS = {
       
    32     # deprecated backend --> replacement module
       
    33     'simple': 'locmem',
       
    34 }
       
    35 
       
    36 def get_cache(backend_uri):
       
    37     if backend_uri.find(':') == -1:
       
    38         raise InvalidCacheBackendError, "Backend URI must start with scheme://"
       
    39     scheme, rest = backend_uri.split(':', 1)
       
    40     if not rest.startswith('//'):
       
    41         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 
       
    50     host = rest[2:]
       
    51     qpos = rest.find('?')
       
    52     if qpos != -1:
       
    53         params = dict(parse_qsl(rest[qpos+1:]))
       
    54         host = rest[2:qpos]
       
    55     else:
       
    56         params = {}
       
    57     if host.endswith('/'):
       
    58         host = host[:-1]
       
    59 
       
    60     cache_class = getattr(__import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']), 'CacheClass')
       
    61     return cache_class(host, params)
       
    62 
       
    63 cache = get_cache(settings.CACHE_BACKEND)