用于选择性缓存/记忆的装饰器 [英] Decorators for selective caching / memoization

查看:90
本文介绍了用于选择性缓存/记忆的装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种构建装饰器@memoize的方法,该方法可以在以下函数中使用:

I am looking for a way of building a decorator @memoize that I can use in functions as follows:

@memoize
my_function(a, b, c):
    # Do stuff 
    # result may not always be the same for fixed (a,b,c)
return result

然后,如果我这样做:

result1 = my_function(a=1,b=2,c=3)
# The function f runs (slow). We cache the result for later

result2 = my_function(a=1, b=2, c=3)
# The decorator reads the cache and returns the result (fast)

现在说我想强制缓存更新:

result3 = my_function(a=1, b=2, c=3, force_update=True)
# The function runs *again* for values a, b, and c. 

result4 = my_function(a=1, b=2, c=3)
# We read the cache

在上面的最后,我们总是有result4 = result3,但不一定是result4 = result,这就是为什么需要一个选项来强制对相同的输入参数进行缓存更新的原因.

At the end of the above, we always have result4 = result3, but not necessarily result4 = result, which is why one needs an option to force the cache update for the same input parameters.

我该如何解决这个问题?

How can I approach this problem?

据我所知 joblib 支持.call,这会强制重新运行,但是它不会更新缓存.

As far as I know joblib supports .call, which forces a re-run, but it does not update the cache.

有没有办法让klepto(请参阅@Wally的答案)默认在特定位置缓存其结果? (例如/some/path/)并在多个功能之间共享此位置?例如.我想说

Is there any way to have klepto (see @Wally's answer) cache its results by default under a specific location? (e.g. /some/path/) and share this location across multiple functions? E.g. I would like to say

cache_path = "/some/path/"

,然后@memoize在给定模块中的同一路径下的几个功能.

and then @memoize several functions in a given module under the same path.

推荐答案

我建议查看joblibklepto.两者都有非常可配置的缓存算法,并且可以做您想要的事情.

I would suggest looking at joblib and klepto. Both have very configurable caching algorithms, and may do what you want.

两个都可以为result1result2进行缓存,并且klepto提供对缓存的访问,因此可以pop从本地内存缓存中获取结果(无需将其从存储的存档中删除,在数据库中说).

Both definitely can do the caching for result1 and result2, and klepto provides access to the cache, so one can pop a result from the local memory cache (without removing it from a stored archive, say in a database).

>>> import klepto
>>> from klepto import lru_cache as memoize
>>> from klepto.keymaps import hashmap
>>> hasher = hashmap(algorithm='md5')
>>> @memoize(keymap=hasher)
... def squared(x):
...   print("called")
...   return x**2
... 
>>> squared(1)
called
1
>>> squared(2)
called
4
>>> squared(3)
called
9
>>> squared(2)
4
>>> 
>>> cache = squared.__cache__()
>>> # delete the 'key' for x=2
>>> cache.pop(squared.key(2))
4
>>> squared(2)
called
4

并非您要找的关键字界面,但它具有您要寻找的功能.

Not exactly the keyword interface you were looking for, but it has the functionality you are looking for.

这篇关于用于选择性缓存/记忆的装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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