summaryrefslogtreecommitdiff
path: root/django/core/cache
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2007-10-26 20:47:20 +0000
committerJustin Bronn <jbronn@gmail.com>2007-10-26 20:47:20 +0000
commit4ffbddf92d89c3b31cef90043721184a501cd454 (patch)
treedb8131d40b0a5437270a6b1e8d579113ab3508e8 /django/core/cache
parentf66ee9d0065838a0f6c9c76203a775a78446cdf7 (diff)
gis: Merged revisions 6525-6613 via svnmerge from [repos:django/trunk trunk].
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/cache')
-rw-r--r--django/core/cache/backends/base.py8
-rw-r--r--django/core/cache/backends/db.py8
-rw-r--r--django/core/cache/backends/dummy.py3
-rw-r--r--django/core/cache/backends/filebased.py20
-rw-r--r--django/core/cache/backends/locmem.py7
-rw-r--r--django/core/cache/backends/memcached.py3
-rw-r--r--django/core/cache/backends/simple.py9
7 files changed, 57 insertions, 1 deletions
diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
index bb67399f3b..cd0d7bd103 100644
--- a/django/core/cache/backends/base.py
+++ b/django/core/cache/backends/base.py
@@ -14,6 +14,14 @@ class BaseCache(object):
timeout = 300
self.default_timeout = timeout
+ def add(self, key, value, timeout=None):
+ """
+ Set a value in the cache if the key does not already exist. If
+ timeout is given, that timeout will be used for the key; otherwise
+ the default cache timeout will be used.
+ """
+ raise NotImplementedError
+
def get(self, key, default=None):
"""
Fetch a given key from the cache. If the key does not exist, return
diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py
index 4a0d44a44e..d54677057f 100644
--- a/django/core/cache/backends/db.py
+++ b/django/core/cache/backends/db.py
@@ -38,6 +38,12 @@ class CacheClass(BaseCache):
return pickle.loads(base64.decodestring(row[1]))
def set(self, key, value, timeout=None):
+ return self._base_set('set', key, value, timeout)
+
+ def add(self, key, value, timeout=None):
+ return self._base_set('add', key, value, timeout)
+
+ def _base_set(self, mode, key, value, timeout=None):
if timeout is None:
timeout = self.default_timeout
cursor = connection.cursor()
@@ -50,7 +56,7 @@ class CacheClass(BaseCache):
encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
try:
- if cursor.fetchone():
+ if mode == 'set' and cursor.fetchone():
cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
else:
cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
diff --git a/django/core/cache/backends/dummy.py b/django/core/cache/backends/dummy.py
index 4c64161538..3ff7e1c1b9 100644
--- a/django/core/cache/backends/dummy.py
+++ b/django/core/cache/backends/dummy.py
@@ -6,6 +6,9 @@ class CacheClass(BaseCache):
def __init__(self, *args, **kwargs):
pass
+ def add(self, *args, **kwargs):
+ pass
+
def get(self, key, default=None):
return default
diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
index d5415c8ace..690193ac81 100644
--- a/django/core/cache/backends/filebased.py
+++ b/django/core/cache/backends/filebased.py
@@ -17,6 +17,26 @@ class CacheClass(SimpleCacheClass):
del self._cache
del self._expire_info
+ def add(self, key, value, timeout=None):
+ fname = self._key_to_file(key)
+ if timeout is None:
+ timeout = self.default_timeout
+ try:
+ filelist = os.listdir(self._dir)
+ except (IOError, OSError):
+ self._createdir()
+ filelist = []
+ if len(filelist) > self._max_entries:
+ self._cull(filelist)
+ if os.path.basename(fname) not in filelist:
+ try:
+ f = open(fname, 'wb')
+ now = time.time()
+ pickle.dump(now + timeout, f, 2)
+ pickle.dump(value, f, 2)
+ except (IOError, OSError):
+ pass
+
def get(self, key, default=None):
fname = self._key_to_file(key)
try:
diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
index 4c48c571b7..5998f7bfd5 100644
--- a/django/core/cache/backends/locmem.py
+++ b/django/core/cache/backends/locmem.py
@@ -14,6 +14,13 @@ class CacheClass(SimpleCacheClass):
SimpleCacheClass.__init__(self, host, params)
self._lock = RWLock()
+ def add(self, key, value, timeout=None):
+ self._lock.writer_enters()
+ try:
+ SimpleCacheClass.add(self, key, value, timeout)
+ finally:
+ self._lock.writer_leaves()
+
def get(self, key, default=None):
should_delete = False
self._lock.reader_enters()
diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py
index 52610daef1..096cec0ee0 100644
--- a/django/core/cache/backends/memcached.py
+++ b/django/core/cache/backends/memcached.py
@@ -16,6 +16,9 @@ class CacheClass(BaseCache):
BaseCache.__init__(self, params)
self._cache = memcache.Client(server.split(';'))
+ def add(self, key, value, timeout=0):
+ self._cache.add(key.encode('ascii', 'ignore'), value, timeout or self.default_timeout)
+
def get(self, key, default=None):
val = self._cache.get(smart_str(key))
if val is None:
diff --git a/django/core/cache/backends/simple.py b/django/core/cache/backends/simple.py
index 3fcad8c7ad..ff60d49066 100644
--- a/django/core/cache/backends/simple.py
+++ b/django/core/cache/backends/simple.py
@@ -21,6 +21,15 @@ class CacheClass(BaseCache):
except (ValueError, TypeError):
self._cull_frequency = 3
+ def add(self, key, value, timeout=None):
+ if len(self._cache) >= self._max_entries:
+ self._cull()
+ if timeout is None:
+ timeout = self.default_timeout
+ if key not in self._cache.keys():
+ self._cache[key] = value
+ self._expire_info[key] = time.time() + timeout
+
def get(self, key, default=None):
now = time.time()
exp = self._expire_info.get(key)