TimedCache now only accepts values expressed in seconds stable
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Wed, 02 Jun 2010 17:23:42 +0000
branchstable
changeset 5642 6a90357b9769
parent 5640 8a6d14f4fb9d
child 5643 fd240f98a3ee
child 5644 73d8a757db80
TimedCache now only accepts values expressed in seconds updated ldapuser.py and pyrorql.py to that new interface.
server/sources/__init__.py
server/sources/ldapuser.py
server/sources/pyrorql.py
--- a/server/sources/__init__.py	Wed Jun 02 16:25:12 2010 +0000
+++ b/server/sources/__init__.py	Wed Jun 02 17:23:42 2010 +0000
@@ -52,11 +52,11 @@
     return True
 
 class TimedCache(dict):
-    def __init__(self, ttlm, ttls=0):
-        # time to live in minutes
-        self.ttl = timedelta(seconds=ttlm*60 + ttls)
-        if self.ttl.seconds <= 0:
+    def __init__(self, ttl):
+        # time to live in seconds
+        if ttl <= 0:
             raise ValueError('TimedCache initialized with a ttl of %ss' % self.ttl.seconds)
+        self.ttl = timedelta(seconds=ttl)
 
     def __setitem__(self, key, value):
         dict.__setitem__(self, key, (datetime.now(), value))
--- a/server/sources/ldapuser.py	Wed Jun 02 16:25:12 2010 +0000
+++ b/server/sources/ldapuser.py	Wed Jun 02 17:23:42 2010 +0000
@@ -188,18 +188,19 @@
         self._conn = None
         self._cache = {}
         # ttlm is in minutes!
-        ttlm = time_validator(None, None,
-                              source_config.get('cache-life-time', 2*60*60)) // 60
-        self._query_cache = TimedCache(max(ttlm, 1))
+        self._cache_ttl = time_validator(None, None,
+                              source_config.get('cache-life-time', 2*60*60))
+        self._cache_ttl = max(71, self._cache_ttl)
+        self._query_cache = TimedCache(self.cache_ttl)
         # interval is in seconds !
         self._interval = time_validator(None, None,
-                                        source_config.get('synchronization-interval',
-                                               24*60*60))
+                                    source_config.get('synchronization-interval',
+                                                      24*60*60))
 
     def reset_caches(self):
         """method called during test to reset potential source caches"""
         self._cache = {}
-        self._query_cache = TimedCache(2*60) # TimedCache is in minutes!
+        self._query_cache = TimedCache(self._cache_ttl)
 
     def init(self):
         """method called by the repository once ready to handle request"""
@@ -207,7 +208,7 @@
         # set minimum period of 5min 1s (the additional second is to minimize
         # resonnance effet)
         self.repo.looping_task(max(301, self._interval), self.synchronize)
-        self.repo.looping_task(max(7, self._query_cache.ttl.seconds // 10),
+        self.repo.looping_task(self.cache_ttl // 10,
                                self._query_cache.clear_expired)
 
     def synchronize(self):
--- a/server/sources/pyrorql.py	Wed Jun 02 16:25:12 2010 +0000
+++ b/server/sources/pyrorql.py	Wed Jun 02 17:23:42 2010 +0000
@@ -144,11 +144,11 @@
                        'group': 'sources',
                        }),)
         register_persistent_options(myoptions)
-        self._query_cache = TimedCache(30)
+        self._query_cache = TimedCache(1800)
 
     def reset_caches(self):
         """method called during test to reset potential source caches"""
-        self._query_cache = TimedCache(30)
+        self._query_cache = TimedCache(1800)
 
     def last_update_time(self):
         pkey = u'sources.%s.latest-update-time' % self.uri