summaryrefslogtreecommitdiff
path: root/django/core/cache
diff options
context:
space:
mode:
authorGary Wilson Jr <gary.wilson@gmail.com>2008-08-02 05:56:57 +0000
committerGary Wilson Jr <gary.wilson@gmail.com>2008-08-02 05:56:57 +0000
commitc85c8f88914cdba05813cc5768377841340bd09d (patch)
tree58f61ee33072b70256a448f7df7333109adf1936 /django/core/cache
parent8a58f2216cb2897db53ad166d71393fef1181c1a (diff)
Fixed #7919 -- md5 and sha modules are deprecated since Python 2.5, use hashlib module when available. Patch from Karen Tracey.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8193 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/cache')
-rw-r--r--django/core/cache/backends/filebased.py29
1 files changed, 15 insertions, 14 deletions
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)
-