Commit b92e5551 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Use the API of Request correctly.

Check the expiration of cached objects only at the first time in each request.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@6127 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 624f4af6
...@@ -72,7 +72,7 @@ class CachingMethod: ...@@ -72,7 +72,7 @@ class CachingMethod:
""" """
# Use this global variable to store cached objects. # Use this global variable to store cached objects.
cached_object_dict = {} cached_object_dict = {}
def __init__(self, callable_object, id = None, cache_duration = 180): def __init__(self, callable_object, id = None, cache_duration = 180):
""" """
callable_object must be callable. callable_object must be callable.
...@@ -96,23 +96,30 @@ class CachingMethod: ...@@ -96,23 +96,30 @@ class CachingMethod:
""" """
global cache_check_time global cache_check_time
now = time() # Store the current time in the REQUEST object, and
# check the expiration only at the first time.
if cache_check_time + CACHE_CHECK_TIMEOUT < now: from Products.ERP5Type.Utils import get_request
# If the time reachs the timeout, expire all old entries. request = get_request()
# XXX this can be quite slow, if many results are cached. now = request.get('_erp5_cache_time', None)
# LOG('CachingMethod', 0, 'checking all entries to expire') if now is None:
cache_check_time = now now = time()
try: request.set('_erp5_cache_time', now)
for index in CachingMethod.cached_object_dict.keys():
obj = CachingMethod.cached_object_dict[index] if cache_check_time + CACHE_CHECK_TIMEOUT < now:
if obj.time + obj.duration < now: # If the time reachs the timeout, expire all old entries.
# LOG('CachingMethod', 0, 'expire %s' % index) # XXX this can be quite slow, if many results are cached.
del CachingMethod.cached_object_dict[index] # LOG('CachingMethod', 0, 'checking all entries to expire')
except KeyError: cache_check_time = now
# This is necessary for multi-threading, because two threads can try:
# delete the same entry at a time. for index in CachingMethod.cached_object_dict.keys():
pass obj = CachingMethod.cached_object_dict[index]
if obj.time + obj.duration < now:
# LOG('CachingMethod', 0, 'expire %s' % index)
del CachingMethod.cached_object_dict[index]
except KeyError:
# This is necessary for multi-threading, because two threads can
# delete the same entry at a time.
pass
key_list = kwd.keys() key_list = kwd.keys()
key_list.sort() key_list.sort()
...@@ -150,23 +157,16 @@ def getTransactionCache(context): ...@@ -150,23 +157,16 @@ def getTransactionCache(context):
"""Get the transaction cache. """Get the transaction cache.
""" """
try: try:
return context.REQUEST._erp5_transaction_cache return context.REQUEST['_erp5_transaction_cache']
except AttributeError: except KeyError:
return None return None
def enableTransactionCache(context): def enableTransactionCache(context):
"""Enable the transaction cache. """Enable the transaction cache.
""" """
try: context.REQUEST.set('_erp5_transaction_cache', {})
context.REQUEST._erp5_transaction_cache = {}
except AttributeError:
pass
def disableTransactionCache(context): def disableTransactionCache(context):
"""Disable the transaction cache. """Disable the transaction cache.
""" """
try: context.REQUEST.set('_erp5_transaction_cache', None)
del context.REQUEST._erp5_transaction_cache
except AttributeError:
pass
\ No newline at end of file
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