Invalidate/prevent memoize with plone.memoize.ram

≯℡__Kan透↙ 提交于 2019-12-09 13:05:39

问题


I've and Zope utility with a method that perform network processes. As the result of the is valid for a while, I'm using plone.memoize.ram to cache the result.

MyClass(object):

    @cache(cache_key)
    def do_auth(self, adapter, data):
        # performing expensive network process here

...and the cache function:

def cache_key(method, utility, data):
    return time() // 60 * 60))

But I want to prevent the memoization to take place when the do_auth call returns empty results (or raise network errors).

Looking at the plone.memoize code it seems I need to raise ram.DontCache() exception, but before doing this I need a way to investigate the old cached value.

How can I get the cached data from the cache storage?


回答1:


I put this together from several code I wrote... It's not tested but may help you.

You may access the cached data using the ICacheChooser utility. It's call method needs the dotted name to the function you cached, in your case itself

key = '{0}.{1}'.format(__name__, method.__name__)
cache = getUtility(ICacheChooser)(key)
storage = cache.ramcache._getStorage()._data
cached_infos = storage.get(key)

In cached_infos there should be all infos you need.



来源:https://stackoverflow.com/questions/37182091/invalidate-prevent-memoize-with-plone-memoize-ram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!