2016-05-12 26 views
6

我和Zope實用程序使用執行網絡進程的方法。 由於該結果有效一段時間,我正在使用plone.memoize.ram來緩存結果。使用plone.memoize.ram無效/防止記憶

MyClass(object): 

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

...和緩存功能:

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

但我想,以防止記憶化時do_auth調用返回空的結果發生(或提高網絡錯誤)。

看着plone.memoize代碼,它似乎我需要raise ram.DontCache()異常,但在做這個之前,我需要一種方法來調查舊的緩存值。

如何從緩存中獲取緩存數據?

回答

3

我把這幾個代碼我寫在一起... 它沒有測試,但可能會幫助你。

您可以使用ICacheChooser實用程序訪問緩存的數據。 它的調用方法需要帶點名稱爲您緩存的功能,在您的案件本身

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

在cached_infos應該有你需要的所有相關信息。