diff options
author | Justin Bronn <jbronn@gmail.com> | 2008-08-05 17:15:33 +0000 |
---|---|---|
committer | Justin Bronn <jbronn@gmail.com> | 2008-08-05 17:15:33 +0000 |
commit | aa239e3e5405933af6a29dac3cf587b59a099927 (patch) | |
tree | ea2cbd139c9a8cf84c09e0b2008bff70e05927ef /django/core/cache/backends | |
parent | 45b73c9a4685809236f84046cc7ffd32a50db958 (diff) | |
download | django-attic/gis.tar.gz |
gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.archive/attic/gisattic/gis
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@8215 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/cache/backends')
-rw-r--r-- | django/core/cache/backends/base.py | 9 | ||||
-rw-r--r-- | django/core/cache/backends/filebased.py | 29 | ||||
-rw-r--r-- | django/core/cache/backends/locmem.py | 2 |
3 files changed, 24 insertions, 16 deletions
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: |