Commits

Mike Bayer  committed 8c75241

* cache.get_value() now uses the given **kwargs in all cases in the same
manner as cache.set_value(). This way you can send a new createfunc
to cache.get_value() each time and it will be used.

  • Participants
  • Parent commits 1bbdf62

Comments (0)

Files changed (3)

 * Added cookie delete to both cookie only sessions and normal sessions, to
   help with proxies and such that may determine whether a user is logged in
   via a cookie. (cookie varies, etc.). Suggested by Felix Schwarz.
-
+* cache.get_value() now uses the given **kwargs in all cases in the same
+  manner as cache.set_value().  This way you can send a new createfunc
+  to cache.get_value() each time and it will be used.
+  
 0.9.5 (6/19/2008)
 * Fixed bug in memcached to be tolerant of keys disappearing when memcached
   expires them.

File beaker/cache.py

         self.kwargs.setdefault('type', 'memory')
     
     def put(self, key, value, **kwargs):
-        kw = self.kwargs.copy()
-        kw.update(kwargs)
         self._values.pop(key, None)
-        self._get_value(key, **kw).set_value(value)
+        self._get_value(key, **kwargs).set_value(value)
     set_value = put
     
     def get(self, key, **kwargs):
-        kw = self.kwargs.copy()
-        kw.update(kwargs)
-        return self._get_value(key, **kw).get_value()
+        return self._get_value(key, **kwargs).get_value()
     get_value = get
     
     def remove_value(self, key, **kwargs):
-        mycontainer = self._get_value(key, **self.kwargs)
+        mycontainer = self._get_value(key, **kwargs)
         if mycontainer.has_current_value():
             mycontainer.clear_value()
 
-    def _get_value(self, key, type, **kwargs):
+    def _get_value(self, key, **kwargs):
         if isinstance(key, unicode):
             key = key.encode('ascii', 'backslashreplace')
-        value = self._values.get(key)
+            
+        if not kwargs:
+            value = self._values.get(key)
+        else:
+            value = None
+            
         if not value:
             kw = self.kwargs.copy()
             kw.update(kwargs)
+            type = kw.pop('type')
             self._values[key] = value = container.Value(key, self.context, self.namespace, clsmap[type], **kw)
         return value
     
         return self.has_key(key)
     
     def has_key(self, key):
-        mycontainer = self._get_value(key, **self.kwargs)
+        mycontainer = self._get_value(key)
         return mycontainer.has_current_value()
     
     def __delitem__(self, key):

File tests/test_cache.py

     time.sleep(3)
     assert not cache.has_key('test')
 
+def test_fresh_createfunc():
+    cache = Cache('test', data_dir='./cache', type='dbm')
+    x = cache.get_value('test', createfunc=lambda: 10, expiretime=2)
+    assert x == 10
+    x = cache.get_value('test', createfunc=lambda: 12, expiretime=2)
+    assert x == 10
+    x = cache.get_value('test', createfunc=lambda: 14, expiretime=2)
+    assert x == 10
+    time.sleep(2)
+    x = cache.get_value('test', createfunc=lambda: 16, expiretime=2)
+    assert x == 16
+    x = cache.get_value('test', createfunc=lambda: 18, expiretime=2)
+    assert x == 16
+    
+    cache.remove_value('test')
+    assert not cache.has_key('test')
+    x = cache.get_value('test', createfunc=lambda: 20, expiretime=2)
+    assert x == 20
+    
+    
+    
 def test_has_key_multicache():
     cache = Cache('test', data_dir='./cache', type='dbm')
     o = object()