Commit 681a65e9 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Use time instead of counting to expire old objects.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@1872 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 11470d55
...@@ -32,8 +32,8 @@ from time import time ...@@ -32,8 +32,8 @@ from time import time
from zLOG import LOG from zLOG import LOG
# XXX need to expire old objects in a way. # XXX need to expire old objects in a way.
cache_check_count = 0 cache_check_time = time()
CACHE_CHECK_MAX = 100 CACHE_CHECK_TIMEOUT = 60
# Use this global variable to store cached objects. # Use this global variable to store cached objects.
cached_object_dict = {} cached_object_dict = {}
...@@ -93,18 +93,18 @@ class CachingMethod: ...@@ -93,18 +93,18 @@ class CachingMethod:
This code looks not aware of multi-threading, but there should no bad effect in reality, This code looks not aware of multi-threading, but there should no bad effect in reality,
since the worst case is that multiple threads compute the same call at a time. since the worst case is that multiple threads compute the same call at a time.
""" """
global cache_check_count global cache_check_time
now = time() now = time()
cache_check_count += 1 if cache_check_time + CACHE_CHECK_TIMEOUT < now:
if cache_check_count >= CACHE_CHECK_MAX: # If the time reachs the timeout, expire all old entries.
# If the count reachs the max, expire all old entries.
# XXX this can be quite slow, if many results are cached. # XXX this can be quite slow, if many results are cached.
LOG('CachingMethod', 0, 'checking all entries to expire') LOG('CachingMethod', 0, 'checking all entries to expire')
cache_check_count = 0 cache_check_time = now
try: try:
for index,obj in cached_object_dict.items(): for index in cached_object_dict.keys():
obj = cached_object_dict[index]
if obj.time + obj.duration < now: if obj.time + obj.duration < now:
LOG('CachingMethod', 0, 'expire %s' % index) LOG('CachingMethod', 0, 'expire %s' % index)
del cached_object_dict[index] del cached_object_dict[index]
......
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