[英]Python check if cache key exists
我有以下使用缓存功能的 python function :
from cachetools import cached, TTLCache
cache = TTLCache(maxsize=100, ttl=3600)
@cached(cache)
def load_data():
# run slow data to get all user data
load_response = requests.request(
'GET',
url=my_url
)
return load_response
有没有办法先检查缓存中是否存在密钥,以便我可以实现其他功能?
当此处不存在缓存键时,我正在尝试实现另一个缓存以从那里获取数据。
在不使用装饰器的情况下像普通字典一样访问缓存
item = cache.get(key, None)
if item is not None:
...
else:
...
# get item the slow way
cache[key] = item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.