Commits

Ben Bangert committed 1eda85f

Adding better cache tests, fixing cache to work properly.

Comments (0)

Files changed (3)

 Beaker Changelog
 ================
 
-0.9.5 (**tip**)
-
+0.9.5 (6/19/2008)
+* Fixed the cache functionality to actually work, previously set_value was
+  ignored if there was already a value set.
 
 0.9.4 (4/13/2008)
 * Adding 'google' backend datastore, available by specifying 'google' as the
         self.kwargs.setdefault('type', 'memory')
     
     def put(self, key, value, **kwargs):
-        kw = self.kwargs
+        kw = self.kwargs.copy()
         kw.update(kwargs)
-        kw.setdefault('type', 'memory')
+        self._containers.pop(key, None)
         self._get_container(key, **kw).set_value(value)
     set_value = put
     
     def get(self, key, **kwargs):
         kw = self.kwargs.copy()
         kw.update(kwargs)
-        kwargs.setdefault('type', 'memory')
         return self._get_container(key, **kw).get_value()
     get_value = get
     

tests/test_cache.py

 # coding: utf-8
+import time
 from beaker.middleware import CacheMiddleware
 from beaker.cache import Cache
 from webtest import TestApp
     cache.remove_value("test")
     assert not cache.has_key("test")
 
+def test_expire_changes():
+    cache = Cache('test', data_dir='./cache', type='dbm')
+    cache.set_value('test', 10)
+    assert cache.has_key('test')
+    assert cache['test'] == 10
+    assert cache._containers['test'].expiretime is None
+    
+    # ensure that we can change a never-expiring value
+    cache.set_value('test', 20, expiretime=1)
+    assert cache.has_key('test')
+    assert cache['test'] == 20
+    assert cache._containers['test'].expiretime == 1
+    time.sleep(1)
+    assert not cache.has_key('test')
+    
+    # test that we can change it before its expired
+    cache.set_value('test', 30, expiretime=50)
+    assert cache.has_key('test')
+    assert cache['test'] == 30
+    assert cache._containers['test'].expiretime == 50
+    
+    cache.set_value('test', 40, expiretime=3)
+    assert cache.has_key('test')
+    assert cache['test'] == 40
+    assert cache._containers['test'].expiretime == 3
+    time.sleep(3)
+    assert not cache.has_key('test')
+
 def test_has_key_multicache():
     cache = Cache('test', data_dir='./cache', type='dbm')
     o = object()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.