gpt4 book ai didi

python - 使用 cachetools 获取 `cache_info()`

转载 作者:行者123 更新时间:2023-12-05 04:51:41 25 4
gpt4 key购买 nike

我正在使用 cachetools我想获取 cache_info():

class UserData(object):
def __init__():
...

@cached(cache=TTLCache(maxsize=1024, ttl=600))
def get_user_data(self, user_id):
return self.redis.get_user_data(user_id)

def get_cache_info():
return self.get_user_data.cache_info()

我想获取缓存统计信息,如 answer 中所示:

>>> foo.cache_info()
CacheInfo(hits=1, misses=1, maxsize=5, currsize=1)

我知道这是一个不同的缓存(我使用的是第 3 方库),但在上面的文档中 cache_info 确实存在。有没有办法以某种方式获取 TTLCache 统计信息?

最佳答案

通过codedocumentation你应该使用 func.py 中的 ttl_cache:

from cachetools.func import ttl_cache


class UserData(object):
...
@ttl_cache(maxsize=1024, ttl=600)
def get_user_data(self, user_id):
return self.redis.get_user_data(user_id)

然后您应该能够调用 UserData.get_user_data.cache_info()

cached 装饰器不维护任何统计信息。

关于python - 使用 cachetools 获取 `cache_info()`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66803745/

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