summaryrefslogtreecommitdiff
path: root/django/core/cache
diff options
context:
space:
mode:
Diffstat (limited to 'django/core/cache')
-rw-r--r--django/core/cache/__init__.py23
-rw-r--r--django/core/cache/backends/base.py9
-rw-r--r--django/core/cache/backends/filebased.py29
-rw-r--r--django/core/cache/backends/locmem.py2
4 files changed, 32 insertions, 31 deletions
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py
index 495cc92822..c136ce4f4d 100644
--- a/django/core/cache/__init__.py
+++ b/django/core/cache/__init__.py
@@ -19,8 +19,10 @@ from cgi import parse_qsl
from django.conf import settings
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 = {
- # name for use in settings file --> name of module in "backends" directory
'memcached': 'memcached',
'locmem': 'locmem',
'file': 'filebased',
@@ -28,24 +30,12 @@ BACKENDS = {
'dummy': 'dummy',
}
-DEPRECATED_BACKENDS = {
- # deprecated backend --> replacement module
- 'simple': 'locmem',
-}
-
def get_cache(backend_uri):
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://"
- if scheme in DEPRECATED_BACKENDS:
- import warnings
- warnings.warn("'%s' backend is deprecated. Use '%s' instead." %
- (scheme, DEPRECATED_BACKENDS[scheme]), DeprecationWarning)
- scheme = DEPRECATED_BACKENDS[scheme]
- if scheme not in BACKENDS:
- raise InvalidCacheBackendError, "%r is not a valid cache backend" % scheme
host = rest[2:]
qpos = rest.find('?')
@@ -57,7 +47,10 @@ def get_cache(backend_uri):
if host.endswith('/'):
host = host[:-1]
- cache_class = getattr(__import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']), 'CacheClass')
- return cache_class(host, params)
+ if scheme in BACKENDS:
+ 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)
diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
index ff4223bf86..58e166d655 100644
--- a/django/core/cache/backends/base.py
+++ b/django/core/cache/backends/base.py
@@ -63,4 +63,11 @@ class BaseCache(object):
"""
return self.get(key) is not None
- __contains__ = has_key
+ def __contains__(self, key):
+ """
+ Returns True if the key is in the cache and has not expired.
+ """
+ # This is a separate method, rather than just a copy of has_key(),
+ # so that it always has the same functionality as has_key(), even
+ # if a subclass overrides it.
+ return self.has_key(key)
diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
index c1277bf20c..0ad586d477 100644
--- a/django/core/cache/backends/filebased.py
+++ b/django/core/cache/backends/filebased.py
@@ -1,29 +1,31 @@
"File-based cache backend"
-import md5
-import os, time
+import os
+import time
try:
import cPickle as pickle
except ImportError:
import pickle
+
from django.core.cache.backends.base import BaseCache
+from django.utils.hashcompat import md5_constructor
class CacheClass(BaseCache):
def __init__(self, dir, params):
BaseCache.__init__(self, params)
-
+
max_entries = params.get('max_entries', 300)
try:
self._max_entries = int(max_entries)
except (ValueError, TypeError):
self._max_entries = 300
-
+
cull_frequency = params.get('cull_frequency', 3)
try:
self._cull_frequency = int(cull_frequency)
except (ValueError, TypeError):
self._cull_frequency = 3
-
+
self._dir = dir
if not os.path.exists(self._dir):
self._createdir()
@@ -31,7 +33,7 @@ class CacheClass(BaseCache):
def add(self, key, value, timeout=None):
if self.has_key(key):
return None
-
+
self.set(key, value, timeout)
def get(self, key, default=None):
@@ -52,12 +54,12 @@ class CacheClass(BaseCache):
def set(self, key, value, timeout=None):
fname = self._key_to_file(key)
dirname = os.path.dirname(fname)
-
+
if timeout is None:
timeout = self.default_timeout
-
+
self._cull()
-
+
try:
if not os.path.exists(dirname):
os.makedirs(dirname)
@@ -103,12 +105,12 @@ class CacheClass(BaseCache):
def _cull(self):
if int(self._num_entries) < self._max_entries:
return
-
+
try:
filelist = os.listdir(self._dir)
except (IOError, OSError):
return
-
+
if self._cull_frequency == 0:
doomed = filelist
else:
@@ -133,11 +135,11 @@ class CacheClass(BaseCache):
Convert the filename into an md5 string. We'll turn the first couple
bits of the path into directory prefixes to be nice to filesystems
that have problems with large numbers of files in a directory.
-
+
Thus, a cache key of "foo" gets turnned into a file named
``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``.
"""
- path = md5.new(key.encode('utf-8')).hexdigest()
+ path = md5_constructor(key.encode('utf-8')).hexdigest()
path = os.path.join(path[:2], path[2:4], path[4:])
return os.path.join(self._dir, path)
@@ -147,4 +149,3 @@ class CacheClass(BaseCache):
count += len(files)
return count
_num_entries = property(_get_num_entries)
-
diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
index e8e1e0d450..053e0735f7 100644
--- a/django/core/cache/backends/locmem.py
+++ b/django/core/cache/backends/locmem.py
@@ -107,7 +107,7 @@ class CacheClass(BaseCache):
else:
doomed = [k for (i, k) in enumerate(self._cache) if i % self._cull_frequency == 0]
for k in doomed:
- self.delete(k)
+ self._delete(k)
def _delete(self, key):
try: