Added a return value to the add() method for caches. It's now possible to tell
if a call to add() ended up storing something in the cache. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8278 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
38dc472796
commit
f6670e1341
|
@ -19,6 +19,8 @@ class BaseCache(object):
|
||||||
Set a value in the cache if the key does not already exist. If
|
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
|
timeout is given, that timeout will be used for the key; otherwise
|
||||||
the default cache timeout will be used.
|
the default cache timeout will be used.
|
||||||
|
|
||||||
|
Returns True if the value was stored, False otherwise.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ class CacheClass(BaseCache):
|
||||||
return pickle.loads(base64.decodestring(row[1]))
|
return pickle.loads(base64.decodestring(row[1]))
|
||||||
|
|
||||||
def set(self, key, value, timeout=None):
|
def set(self, key, value, timeout=None):
|
||||||
return self._base_set('set', key, value, timeout)
|
self._base_set('set', key, value, timeout)
|
||||||
|
|
||||||
def add(self, key, value, timeout=None):
|
def add(self, key, value, timeout=None):
|
||||||
return self._base_set('add', key, value, timeout)
|
return self._base_set('add', key, value, timeout)
|
||||||
|
@ -62,9 +62,10 @@ class CacheClass(BaseCache):
|
||||||
cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
|
cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
|
||||||
except DatabaseError:
|
except DatabaseError:
|
||||||
# To be threadsafe, updates/inserts are allowed to fail silently
|
# To be threadsafe, updates/inserts are allowed to fail silently
|
||||||
pass
|
return False
|
||||||
else:
|
else:
|
||||||
transaction.commit_unless_managed()
|
transaction.commit_unless_managed()
|
||||||
|
return True
|
||||||
|
|
||||||
def delete(self, key):
|
def delete(self, key):
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
|
@ -7,7 +7,7 @@ class CacheClass(BaseCache):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def add(self, *args, **kwargs):
|
def add(self, *args, **kwargs):
|
||||||
pass
|
return True
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
return default
|
return default
|
||||||
|
|
|
@ -32,9 +32,10 @@ class CacheClass(BaseCache):
|
||||||
|
|
||||||
def add(self, key, value, timeout=None):
|
def add(self, key, value, timeout=None):
|
||||||
if self.has_key(key):
|
if self.has_key(key):
|
||||||
return None
|
return False
|
||||||
|
|
||||||
self.set(key, value, timeout)
|
self.set(key, value, timeout)
|
||||||
|
return True
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
fname = self._key_to_file(key)
|
fname = self._key_to_file(key)
|
||||||
|
|
|
@ -36,8 +36,10 @@ class CacheClass(BaseCache):
|
||||||
if exp is None or exp <= time.time():
|
if exp is None or exp <= time.time():
|
||||||
try:
|
try:
|
||||||
self._set(key, pickle.dumps(value), timeout)
|
self._set(key, pickle.dumps(value), timeout)
|
||||||
|
return True
|
||||||
except pickle.PickleError:
|
except pickle.PickleError:
|
||||||
pass
|
pass
|
||||||
|
return False
|
||||||
finally:
|
finally:
|
||||||
self._lock.writer_leaves()
|
self._lock.writer_leaves()
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ class CacheClass(BaseCache):
|
||||||
self._cache = memcache.Client(server.split(';'))
|
self._cache = memcache.Client(server.split(';'))
|
||||||
|
|
||||||
def add(self, key, value, timeout=0):
|
def add(self, key, value, timeout=0):
|
||||||
self._cache.add(key.encode('ascii', 'ignore'), value, timeout or self.default_timeout)
|
return self._cache.add(key.encode('ascii', 'ignore'), value, timeout or self.default_timeout)
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
val = self._cache.get(smart_str(key))
|
val = self._cache.get(smart_str(key))
|
||||||
|
|
|
@ -410,9 +410,13 @@ it will not attempt to update the cache if the key specified is already present:
|
||||||
>>> cache.get('add_key')
|
>>> cache.get('add_key')
|
||||||
'Initial value'
|
'Initial value'
|
||||||
|
|
||||||
There's also a ``get_many()`` interface that only hits the cache once. ``get_many()``
|
If you need to know whether ``add()`` stored a value in the cache, you can
|
||||||
returns a dictionary with all the keys you asked for that actually exist in the
|
check the return value. It will return ``True`` if the value was stored,
|
||||||
cache (and haven't expired)::
|
``False`` otherwise.
|
||||||
|
|
||||||
|
There's also a ``get_many()`` interface that only hits the cache once.
|
||||||
|
``get_many()`` returns a dictionary with all the keys you asked for that
|
||||||
|
actually exist in the cache (and haven't expired)::
|
||||||
|
|
||||||
>>> cache.set('a', 1)
|
>>> cache.set('a', 1)
|
||||||
>>> cache.set('b', 2)
|
>>> cache.set('b', 2)
|
||||||
|
|
|
@ -31,7 +31,8 @@ class Cache(unittest.TestCase):
|
||||||
def test_add(self):
|
def test_add(self):
|
||||||
# test add (only add if key isn't already in cache)
|
# test add (only add if key isn't already in cache)
|
||||||
cache.add("addkey1", "value")
|
cache.add("addkey1", "value")
|
||||||
cache.add("addkey1", "newvalue")
|
result = cache.add("addkey1", "newvalue")
|
||||||
|
self.assertEqual(result, False)
|
||||||
self.assertEqual(cache.get("addkey1"), "value")
|
self.assertEqual(cache.get("addkey1"), "value")
|
||||||
|
|
||||||
def test_non_existent(self):
|
def test_non_existent(self):
|
||||||
|
|
Loading…
Reference in New Issue