[Erp5-report] r21104 - /erp5/trunk/products/ERP5Type/CachePlugins/

nobody at svn.erp5.org nobody at svn.erp5.org
Fri May 23 16:35:01 CEST 2008


Author: vincent
Date: Fri May 23 16:34:48 2008
New Revision: 21104

URL: http://svn.erp5.org?rev=21104&view=rev
Log:
When scheduling expirations, it's more efficient to store expiration date over storing creation date + life duration, and computing expiration date at every expiration check. Apply this to
 BaseCache and its users, RamCache and SQLCache.
Also, don't cast timestamps to integers (it's not needed, so just don't do it. cache is here to save cpu time).

Modified:
    erp5/trunk/products/ERP5Type/CachePlugins/BaseCache.py
    erp5/trunk/products/ERP5Type/CachePlugins/RamCache.py
    erp5/trunk/products/ERP5Type/CachePlugins/SQLCache.py

Modified: erp5/trunk/products/ERP5Type/CachePlugins/BaseCache.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/CachePlugins/BaseCache.py?rev=21104&r1=21103&r2=21104&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/CachePlugins/BaseCache.py (original)
+++ erp5/trunk/products/ERP5Type/CachePlugins/BaseCache.py Fri May 23 16:34:48 2008
@@ -38,8 +38,7 @@
 class CacheEntry(object):
   """ Cachable entry.  Used as a wrapper around real values stored in cache.
   value
-  cache_duration
-  stored_at
+  expires_at
   cache_hits
   calculation_time
   TODO: Based on above data we can have a different invalidation policy
@@ -47,22 +46,16 @@
     
   def __init__(self, value, cache_duration=None, calculation_time=0):
     self.value = value
-    self.cache_duration = cache_duration
-    self.stored_at = int(time.time())
+    if cache_duration in (None, 0):
+      self.expires_at = None
+    else:
+      self.expires_at = time.time() + cache_duration
     self.cache_hits = 0
     self.calculation_time = calculation_time
 
-       
   def isExpired(self):
     """ check cache entry for expiration """
-    if self.cache_duration is None or self.cache_duration==0:
-      ## cache entry can stay in cache forever until zope restarts
-      return False
-    now = int(time.time())
-    if now > (self.stored_at + int(self.cache_duration)):
-      return True
-    else:
-      return False
+    return self.expires_at < time.time() or self.expires_at is None
     
   def markCacheHit(self, delta=1):
     """ mark a read to this cache entry """
@@ -81,7 +74,7 @@
   cache_expire_check_interval = 60
     
   def __init__(self, params={}):
-    self._last_cache_expire_check_at = time.time()
+    self._next_cache_expire_check_at = time.time()
     self._cache_hits = 0
     self._cache_misses = 0
         

Modified: erp5/trunk/products/ERP5Type/CachePlugins/RamCache.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/CachePlugins/RamCache.py?rev=21104&r1=21103&r2=21104&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/CachePlugins/RamCache.py (original)
+++ erp5/trunk/products/ERP5Type/CachePlugins/RamCache.py Fri May 23 16:34:48 2008
@@ -83,9 +83,9 @@
 
   def expireOldCacheEntries(self, forceCheck = False):
     now = time.time()
-    if forceCheck or (now > (self._last_cache_expire_check_at + self.cache_expire_check_interval)):
+    if forceCheck or (now > self._next_cache_expire_check_at):
       ## time to check for expired cache items
-      self._last_cache_expire_check_at = now
+      self._next_cache_expire_check_at = now + self.cache_expire_check_interval
       cache = self.getCacheStorage()        
       for key, value in cache.items():
         if value.isExpired():

Modified: erp5/trunk/products/ERP5Type/CachePlugins/SQLCache.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/CachePlugins/SQLCache.py?rev=21104&r1=21103&r2=21104&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/CachePlugins/SQLCache.py (original)
+++ erp5/trunk/products/ERP5Type/CachePlugins/SQLCache.py Fri May 23 16:34:48 2008
@@ -201,9 +201,9 @@
             
   def expireOldCacheEntries(self, forceCheck = False):
     now = time.time()
-    if forceCheck or (now > (self._last_cache_expire_check_at + self.cache_expire_check_interval)):
+    if forceCheck or (now > self._next_cache_expire_check_at):
       ## time to check for expired cache items
-      self._last_cache_expire_check_at = now
+      self._next_cache_expire_check_at = now + self.cache_expire_check_interval
       my_query = self.delete_expired_keys_sql %(self._db_cache_table_name, now)
       self.execSQLQuery(my_query)
 




More information about the Erp5-report mailing list