gpt4 book ai didi

python - lru_cache - 是否可以只清除缓存中的特定调用?

转载 作者:行者123 更新时间:2023-11-28 17:59:34 24 4
gpt4 key购买 nike

我知道 cache_clear。但是我想知道如何清除单个调用的缓存。看到 cache_clear 不接受参数,我认为唯一的方法是直接弄乱底层字典。会有什么副作用(如果有的话)?

我们的目标是实现以下目标

from functools import lru_cache

@lru_cache()
def f(x):
print('called with ', x)


f(1)
f.cache_clear(1) # obviously does not work
f(1)

输出

called with  1
called with 1

最佳答案

这应该被认为是不可能的。对此没有公共(public) API,根本没有访问缓存的属性,也没有缓存键的统一格式。即使你采取极端措施(gc/ctypes shenanigans)来获取缓存,你也可能会破坏整个 LRU 系统,泄漏大量内存无处不在,也许还有段错误。 implementation中的内存管理面对直接破坏缓存的人,其设计并不安全。

关于python - lru_cache - 是否可以只清除缓存中的特定调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56413413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com