2006-02-24 14:07:01 +08:00
|
|
|
"File-based cache backend"
|
|
|
|
|
2008-08-02 13:56:57 +08:00
|
|
|
import os
|
|
|
|
import time
|
2010-01-27 16:21:35 +08:00
|
|
|
import shutil
|
2006-02-24 14:07:01 +08:00
|
|
|
try:
|
|
|
|
import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2007-12-02 08:35:14 +08:00
|
|
|
from django.core.cache.backends.base import BaseCache
|
2008-08-02 13:56:57 +08:00
|
|
|
from django.utils.hashcompat import md5_constructor
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2007-12-02 08:35:14 +08:00
|
|
|
class CacheClass(BaseCache):
|
2010-11-19 23:39:35 +08:00
|
|
|
def __init__(self, dir, params, key_prefix='', version=1, key_func=None):
|
|
|
|
BaseCache.__init__(self, params, key_prefix, version, key_func)
|
2006-02-24 14:07:01 +08:00
|
|
|
self._dir = dir
|
|
|
|
if not os.path.exists(self._dir):
|
|
|
|
self._createdir()
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def add(self, key, value, timeout=None, version=None):
|
|
|
|
if self.has_key(key, version=version):
|
2008-08-10 11:52:21 +08:00
|
|
|
return False
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
self.set(key, value, timeout, version=version)
|
2008-08-10 11:52:21 +08:00
|
|
|
return True
|
2007-10-20 23:16:34 +08:00
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def get(self, key, default=None, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2010-11-19 23:39:35 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
fname = self._key_to_file(key)
|
|
|
|
try:
|
|
|
|
f = open(fname, 'rb')
|
2010-04-16 19:11:55 +08:00
|
|
|
try:
|
|
|
|
exp = pickle.load(f)
|
|
|
|
now = time.time()
|
|
|
|
if exp < now:
|
|
|
|
self._delete(fname)
|
|
|
|
else:
|
|
|
|
return pickle.load(f)
|
|
|
|
finally:
|
2006-02-24 14:07:01 +08:00
|
|
|
f.close()
|
|
|
|
except (IOError, OSError, EOFError, pickle.PickleError):
|
|
|
|
pass
|
|
|
|
return default
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def set(self, key, value, timeout=None, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2010-11-19 23:39:35 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
fname = self._key_to_file(key)
|
2007-12-05 02:03:56 +08:00
|
|
|
dirname = os.path.dirname(fname)
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
if timeout is None:
|
|
|
|
timeout = self.default_timeout
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2007-12-05 02:03:56 +08:00
|
|
|
self._cull()
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
try:
|
2007-12-05 02:03:56 +08:00
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
f = open(fname, 'wb')
|
2010-04-16 19:11:55 +08:00
|
|
|
try:
|
|
|
|
now = time.time()
|
|
|
|
pickle.dump(now + timeout, f, pickle.HIGHEST_PROTOCOL)
|
|
|
|
pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
|
|
|
|
finally:
|
|
|
|
f.close()
|
2006-02-24 14:07:01 +08:00
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def delete(self, key, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2006-02-24 14:07:01 +08:00
|
|
|
try:
|
2007-12-05 02:03:56 +08:00
|
|
|
self._delete(self._key_to_file(key))
|
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _delete(self, fname):
|
|
|
|
os.remove(fname)
|
|
|
|
try:
|
|
|
|
# Remove the 2 subdirs if they're empty
|
|
|
|
dirname = os.path.dirname(fname)
|
|
|
|
os.rmdir(dirname)
|
|
|
|
os.rmdir(os.path.dirname(dirname))
|
2006-02-24 14:07:01 +08:00
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def has_key(self, key, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2007-12-05 02:03:56 +08:00
|
|
|
fname = self._key_to_file(key)
|
|
|
|
try:
|
|
|
|
f = open(fname, 'rb')
|
2010-04-16 19:11:55 +08:00
|
|
|
try:
|
|
|
|
exp = pickle.load(f)
|
|
|
|
now = time.time()
|
|
|
|
if exp < now:
|
|
|
|
self._delete(fname)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
finally:
|
2007-12-05 02:03:56 +08:00
|
|
|
f.close()
|
|
|
|
except (IOError, OSError, EOFError, pickle.PickleError):
|
|
|
|
return False
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2007-12-05 02:03:56 +08:00
|
|
|
def _cull(self):
|
|
|
|
if int(self._num_entries) < self._max_entries:
|
|
|
|
return
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2007-12-05 02:03:56 +08:00
|
|
|
try:
|
2010-09-10 21:03:02 +08:00
|
|
|
filelist = sorted(os.listdir(self._dir))
|
2007-12-05 02:03:56 +08:00
|
|
|
except (IOError, OSError):
|
|
|
|
return
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
if self._cull_frequency == 0:
|
|
|
|
doomed = filelist
|
|
|
|
else:
|
2007-12-05 02:03:56 +08:00
|
|
|
doomed = [os.path.join(self._dir, k) for (i, k) in enumerate(filelist) if i % self._cull_frequency == 0]
|
|
|
|
|
|
|
|
for topdir in doomed:
|
2006-02-24 14:07:01 +08:00
|
|
|
try:
|
2007-12-05 02:03:56 +08:00
|
|
|
for root, _, files in os.walk(topdir):
|
|
|
|
for f in files:
|
|
|
|
self._delete(os.path.join(root, f))
|
2006-02-24 14:07:01 +08:00
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _createdir(self):
|
|
|
|
try:
|
|
|
|
os.makedirs(self._dir)
|
|
|
|
except OSError:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise EnvironmentError("Cache directory '%s' does not exist and could not be created'" % self._dir)
|
2006-02-24 14:07:01 +08:00
|
|
|
|
|
|
|
def _key_to_file(self, key):
|
2007-12-05 02:03:56 +08:00
|
|
|
"""
|
|
|
|
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.
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2007-12-05 02:03:56 +08:00
|
|
|
Thus, a cache key of "foo" gets turnned into a file named
|
|
|
|
``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``.
|
|
|
|
"""
|
2010-11-19 23:39:35 +08:00
|
|
|
path = md5_constructor(key).hexdigest()
|
2007-12-05 02:03:56 +08:00
|
|
|
path = os.path.join(path[:2], path[2:4], path[4:])
|
|
|
|
return os.path.join(self._dir, path)
|
|
|
|
|
|
|
|
def _get_num_entries(self):
|
|
|
|
count = 0
|
|
|
|
for _,_,files in os.walk(self._dir):
|
|
|
|
count += len(files)
|
|
|
|
return count
|
|
|
|
_num_entries = property(_get_num_entries)
|
2010-01-27 16:21:35 +08:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
try:
|
|
|
|
shutil.rmtree(self._dir)
|
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|