使用plone.memoize.ram无效/阻止记忆 [英] Invalidate/prevent memoize with plone.memoize.ram

查看:92
本文介绍了使用plone.memoize.ram无效/阻止记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Zope实用程序,该实用程序具有执行网络进程的方法. 由于的结果有效了一段时间,因此我正在使用plone.memoize.ram来缓存结果.

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

但是我想防止在do_auth调用返回空结果(或引发网络错误)时进行记录.

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

看一下plone.memoize代码,看来我需要raise ram.DontCache()异常,但是在执行此操作之前,我需要一种方法来调查旧的缓存值.

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?

推荐答案

我从我编写的一些代码中总结了这些内容... 它未经测试,但可能会对您有所帮助.

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

您可以使用ICacheChooser实用程序访问缓存的数据. 调用方法需要您缓存的函数的点名,以您自己为例

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)

在cached_infos中应该有您需要的所有信息.

In cached_infos there should be all infos you need.

这篇关于使用plone.memoize.ram无效/阻止记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆