Commit e0802dfe authored by Vincent Pelletier's avatar Vincent Pelletier

When scheduling expirations, it's more efficient to store expiration date over...

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).


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21104 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0a20e879
...@@ -38,8 +38,7 @@ class CachedMethodError(Exception): ...@@ -38,8 +38,7 @@ class CachedMethodError(Exception):
class CacheEntry(object): class CacheEntry(object):
""" Cachable entry. Used as a wrapper around real values stored in cache. """ Cachable entry. Used as a wrapper around real values stored in cache.
value value
cache_duration expires_at
stored_at
cache_hits cache_hits
calculation_time calculation_time
TODO: Based on above data we can have a different invalidation policy TODO: Based on above data we can have a different invalidation policy
...@@ -47,22 +46,16 @@ class CacheEntry(object): ...@@ -47,22 +46,16 @@ class CacheEntry(object):
def __init__(self, value, cache_duration=None, calculation_time=0): def __init__(self, value, cache_duration=None, calculation_time=0):
self.value = value self.value = value
self.cache_duration = cache_duration if cache_duration in (None, 0):
self.stored_at = int(time.time()) self.expires_at = None
else:
self.expires_at = time.time() + cache_duration
self.cache_hits = 0 self.cache_hits = 0
self.calculation_time = calculation_time self.calculation_time = calculation_time
def isExpired(self): def isExpired(self):
""" check cache entry for expiration """ """ check cache entry for expiration """
if self.cache_duration is None or self.cache_duration==0: return self.expires_at < time.time() or self.expires_at is None
## 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
def markCacheHit(self, delta=1): def markCacheHit(self, delta=1):
""" mark a read to this cache entry """ """ mark a read to this cache entry """
...@@ -81,7 +74,7 @@ class BaseCache(object): ...@@ -81,7 +74,7 @@ class BaseCache(object):
cache_expire_check_interval = 60 cache_expire_check_interval = 60
def __init__(self, params={}): 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_hits = 0
self._cache_misses = 0 self._cache_misses = 0
......
...@@ -83,9 +83,9 @@ class RamCache(BaseCache): ...@@ -83,9 +83,9 @@ class RamCache(BaseCache):
def expireOldCacheEntries(self, forceCheck = False): def expireOldCacheEntries(self, forceCheck = False):
now = time.time() 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 ## 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() cache = self.getCacheStorage()
for key, value in cache.items(): for key, value in cache.items():
if value.isExpired(): if value.isExpired():
......
...@@ -201,9 +201,9 @@ class SQLCache(BaseCache): ...@@ -201,9 +201,9 @@ class SQLCache(BaseCache):
def expireOldCacheEntries(self, forceCheck = False): def expireOldCacheEntries(self, forceCheck = False):
now = time.time() 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 ## 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) my_query = self.delete_expired_keys_sql %(self._db_cache_table_name, now)
self.execSQLQuery(my_query) self.execSQLQuery(my_query)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment