Ticket #4831: 4831-1.diff
File 4831-1.diff, 8.4 KB (added by , 17 years ago) |
---|
-
django/core/cache/backends/simple.py
21 21 except (ValueError, TypeError): 22 22 self._cull_frequency = 3 23 23 24 def add(self, key, value, timeout=None): 25 if len(self._cache) >= self._max_entries: 26 self._cull() 27 if timeout is None: 28 timeout = self.default_timeout 29 if key not in self._cache.keys(): 30 self._cache[key] = value 31 self._expire_info[key] = time.time() + timeout 32 24 33 def get(self, key, default=None): 25 34 now = time.time() 26 35 exp = self._expire_info.get(key) -
django/core/cache/backends/base.py
14 14 timeout = 300 15 15 self.default_timeout = timeout 16 16 17 def add(self, key, value, timeout=None): 18 """ 19 Set a value in the cache if the key does not already exist. If 20 timeout is given, that timeout will be used for the key; otherwise 21 the default cache timeout will be used. 22 """ 23 raise NotImplementedError 24 17 25 def get(self, key, default=None): 18 26 """ 19 27 Fetch a given key from the cache. If the key does not exist, return -
django/core/cache/backends/dummy.py
6 6 def __init__(self, *args, **kwargs): 7 7 pass 8 8 9 def add(self, *args, **kwargs): 10 pass 11 9 12 def get(self, key, default=None): 10 13 return default 11 14 -
django/core/cache/backends/locmem.py
9 9 SimpleCacheClass.__init__(self, host, params) 10 10 self._lock = RWLock() 11 11 12 def add(self, key, value, timeout=None): 13 self._lock.writer_enters() 14 try: 15 SimpleCacheClass.add(self, key, value, timeout) 16 finally: 17 self._lock.writer_leaves() 18 12 19 def get(self, key, default=None): 13 20 should_delete = False 14 21 self._lock.reader_enters() -
django/core/cache/backends/filebased.py
16 16 del self._cache 17 17 del self._expire_info 18 18 19 def add(self, key, value, timeout=None): 20 fname = self._key_to_file(key) 21 if timeout is None: 22 timeout = self.default_timeout 23 try: 24 filelist = os.listdir(self._dir) 25 except (IOError, OSError): 26 self._createdir() 27 filelist = [] 28 if len(filelist) > self._max_entries: 29 self._cull(filelist) 30 if os.path.basename(fname) not in filelist: 31 try: 32 f = open(fname, 'wb') 33 now = time.time() 34 pickle.dump(now + timeout, f, 2) 35 pickle.dump(value, f, 2) 36 except (IOError, OSError): 37 pass 38 19 39 def get(self, key, default=None): 20 40 fname = self._key_to_file(key) 21 41 try: -
django/core/cache/backends/db.py
24 24 except (ValueError, TypeError): 25 25 self._cull_frequency = 3 26 26 27 def add(self, key, value, timeout=None): 28 return self._base_set('add', key, value, timeout) 29 27 30 def get(self, key, default=None): 28 31 cursor = connection.cursor() 29 32 cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key]) … … 38 41 return pickle.loads(base64.decodestring(row[1])) 39 42 40 43 def set(self, key, value, timeout=None): 44 return self._base_set('set', key, value, timeout) 45 46 def delete(self, key): 47 cursor = connection.cursor() 48 cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 49 transaction.commit_unless_managed() 50 51 def has_key(self, key): 52 cursor = connection.cursor() 53 cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) 54 return cursor.fetchone() is not None 55 56 def _base_set(self, mode, key, value, timeout=None): 41 57 if timeout is None: 42 58 timeout = self.default_timeout 43 59 cursor = connection.cursor() … … 50 66 encoded = base64.encodestring(pickle.dumps(value, 2)).strip() 51 67 cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) 52 68 try: 53 if cursor.fetchone():69 if mode == 'set' and cursor.fetchone(): 54 70 cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key]) 55 71 else: 56 cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) 72 if mode == 'add': 73 cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) 57 74 except DatabaseError: 58 75 # To be threadsafe, updates/inserts are allowed to fail silently 59 76 pass 60 77 else: 61 78 transaction.commit_unless_managed() 62 79 63 def delete(self, key):64 cursor = connection.cursor()65 cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])66 transaction.commit_unless_managed()67 68 def has_key(self, key):69 cursor = connection.cursor()70 cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])71 return cursor.fetchone() is not None72 73 80 def _cull(self, cursor, now): 74 81 if self._cull_frequency == 0: 75 82 cursor.execute("DELETE FROM %s" % self._table) -
django/core/cache/backends/memcached.py
15 15 BaseCache.__init__(self, params) 16 16 self._cache = memcache.Client(server.split(';')) 17 17 18 def add(self, key, value, timeout=0): 19 self._cache.add(key.encode('ascii', 'ignore'), value, timeout or self.default_timeout) 20 18 21 def get(self, key, default=None): 19 22 val = self._cache.get(key) 20 23 if val is None: -
tests/regressiontests/cache/tests.py
17 17 cache.set("key", "value") 18 18 self.assertEqual(cache.get("key"), "value") 19 19 20 def test_add(self): 21 # test add (only add if key isn't already in cache) 22 cache.add("addkey1", "value") 23 cache.add("addkey1", "newvalue") 24 self.assertEqual(cache.get("addkey1"), "value") 25 20 26 def test_non_existent(self): 21 27 # get with non-existent keys 22 28 self.assertEqual(cache.get("does not exist"), None) -
docs/cache.txt
324 324 >>> cache.get('my_key', 'has expired') 325 325 'has expired' 326 326 327 To add a key only if it doesn't already exist, there is an add() method. It 328 takes the same parameters as set(), but will not attempt to update the cache 329 if the key specified is already present:: 330 331 >>> cache.set('add_key', 'Initial value') 332 >>> cache.add('add_key', 'New value') 333 >>> cache.get('add_key') 334 'Initial value' 335 327 336 There's also a get_many() interface that only hits the cache once. get_many() 328 337 returns a dictionary with all the keys you asked for that actually exist in the 329 338 cache (and haven't expired)::