server/sources/__init__.py
changeset 1238 fa29b5b60107
parent 386 7af259b73c5b
child 1250 5c20a7f13c84
--- a/server/sources/__init__.py	Sat Apr 04 15:16:37 2009 +0200
+++ b/server/sources/__init__.py	Sat Apr 04 15:45:57 2009 +0200
@@ -1,15 +1,35 @@
 """cubicweb server sources support
 
 :organization: Logilab
-:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 __docformat__ = "restructuredtext en"
 
 from logging import getLogger
 
+from mx.DateTime import now, DateTimeDelta
+
 from cubicweb import set_log_methods
 
+class TimedCache(dict):
+    def __init__(self, ttlm, ttls=0):
+        # time to live in minutes
+        self.ttl = DateTimeDelta(0, 0, ttlm, ttls)
+        
+    def __setitem__(self, key, value):
+        dict.__setitem__(self, key, (now(), value))
+        
+    def __getitem__(self, key):
+        return dict.__getitem__(self, key)[1]
+    
+    def clear_expired(self):
+        now_ = now()
+        ttl = self.ttl
+        for key, (timestamp, value) in self.items():
+            if now_ - timestamp > ttl:
+                del self[key]
+
 
 class AbstractSource(object):
     """an abstract class for sources"""