Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed create_cache function.

  • Loading branch information...
commit 35e289fe9285feffed3c60657af9279a6a2cfccc 1 parent 8e274f7
Aymeric Augustin authored November 23, 2013

Showing 1 changed file with 38 additions and 47 deletions. Show diff stats Hide diff stats

  1. 85  django/core/cache/__init__.py
85  django/core/cache/__init__.py
@@ -24,8 +24,8 @@
24 24
 
25 25
 
26 26
 __all__ = [
27  
-    'create_cache', 'get_cache', 'cache', 'DEFAULT_CACHE_ALIAS',
28  
-    'InvalidCacheBackendError', 'CacheKeyWarning', 'BaseCache',
  27
+    'get_cache', 'cache', 'DEFAULT_CACHE_ALIAS', 'InvalidCacheBackendError',
  28
+    'CacheKeyWarning', 'BaseCache',
29 29
 ]
30 30
 
31 31
 DEFAULT_CACHE_ALIAS = 'default'
@@ -36,61 +36,49 @@
36 36
 
37 37
 def get_cache(backend, **kwargs):
38 38
     """
39  
-    Function to retrieve a configure cache, or create a new one.
40  
-
41  
-    This wrapper is for backward compatibility.
42  
-
43  
-    Use either create_cache or caches directly.
44  
-
45  
-    """
46  
-    warnings.warn("'get_cache' is deprecated.  Use either caches or create_cache.",
47  
-        PendingDeprecationWarning, stacklevel=2)
48  
-
49  
-    # If it's just an alias with no options, use the new API
50  
-    if backend in settings.CACHES and not kwargs:
51  
-        return caches[backend]
52  
-
53  
-    return create_cache(backend, **kwargs)
54  
-
55  
-
56  
-def create_cache(backend, **params):
57  
-    """
58  
-    Function to create a cache backend dynamically.  This is flexible by design
  39
+    Function to create a cache backend dynamically. This is flexible by design
59 40
     to allow different use cases:
60 41
 
61  
-    To load a backend with its dotted import path, including options::
62  
-
63  
-        cache = get_cache('django.core.cache.backends.memcached.MemcachedCache',
64  
-            LOCATION='127.0.0.1:11211', TIMEOUT=30,
65  
-        })
66  
-
67  
-    To create a new instance of a cache in settings.CACHES, pass the alias::
  42
+    To load a backend that is pre-defined in the settings::
68 43
 
69  
-        cache = create_cache('default')
  44
+        cache = get_cache('default')
70 45
 
71  
-    You can also pass extra parameters to override those in settings.CACHES::
  46
+    To create a backend with its dotted import path,
  47
+    including arbitrary options::
72 48
 
73  
-        cache = create_cache('default', LOCATION='bar')
  49
+        cache = get_cache('django.core.cache.backends.memcached.MemcachedCache', **{
  50
+            'LOCATION': '127.0.0.1:11211', 'TIMEOUT': 30,
  51
+        })
74 52
 
75 53
     """
  54
+    warnings.warn("'get_cache' is deprecated in favor of 'caches'.",
  55
+                  PendingDeprecationWarning, stacklevel=2)
  56
+    return _create_cache(backend, **kwargs)
76 57
 
77  
-    # We can name a cache from settings.CACHES and update its params
78  
-    try:
79  
-        conf = settings.CACHES[backend]
80  
-    except KeyError:
81  
-        pass
82  
-    else:
83  
-        params = conf.copy()
84  
-        params.update(params)
85  
-        backend = params.pop('BACKEND')
86 58
 
  59
+def _create_cache(backend, **kwargs):
87 60
     try:
  61
+        # Try to get the CACHES entry for the given backend name first
  62
+        try:
  63
+            conf = settings.CACHES[backend]
  64
+        except KeyError:
  65
+            try:
  66
+                # Trying to import the given backend, in case it's a dotted path
  67
+                import_by_path(backend)
  68
+            except ImproperlyConfigured as e:
  69
+                raise InvalidCacheBackendError("Could not find backend '%s': %s" % (
  70
+                    backend, e))
  71
+            location = kwargs.pop('LOCATION', '')
  72
+            params = kwargs
  73
+        else:
  74
+            params = conf.copy()
  75
+            params.update(kwargs)
  76
+            backend = params.pop('BACKEND')
  77
+            location = params.pop('LOCATION', '')
88 78
         backend_cls = import_by_path(backend)
89  
-    except ImproperlyConfigured as e:
90  
-        raise InvalidCacheBackendError("Could not find backend '%s': %s" % (
91  
-            backend, e
92  
-        ))
93  
-    location = params.pop('LOCATION', '')
  79
+    except (AttributeError, ImportError, ImproperlyConfigured) as e:
  80
+        raise InvalidCacheBackendError(
  81
+            "Could not find backend '%s': %s" % (backend, e))
94 82
     cache = backend_cls(location, params)
95 83
     # Some caches -- python-memcached in particular -- need to do a cleanup at the
96 84
     # end of a request cycle. If not implemented in a particular backend
@@ -119,7 +107,7 @@ def __getitem__(self, alias):
119 107
                 "Could not find config for '%s' in settings.CACHES" % alias
120 108
             )
121 109
 
122  
-        cache = create_cache(alias)
  110
+        cache = _create_cache(alias)
123 111
         setattr(self._caches, alias, cache)
124 112
 
125 113
         return cache
@@ -142,6 +130,9 @@ def __setattr__(self, name, value):
142 130
     def __delattr__(self, name):
143 131
         return delattr(caches[DEFAULT_CACHE_ALIAS], name)
144 132
 
  133
+    def __contains__(self, key):
  134
+        return key in caches[DEFAULT_CACHE_ALIAS]
  135
+
145 136
     def __eq__(self, other):
146 137
         return caches[DEFAULT_CACHE_ALIAS] == other
147 138
 

0 notes on commit 35e289f

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