简体   繁体   English

列出 Flask Cache 中所有缓存的键

[英]List all the cached keys in Flask Cache

I have written an appliacation using Flask, and am caching the response of various api calls.我已经使用 Flask 编写了一个应用程序,并且正在缓存各种 api 调用的响应。 following is the configuration of my flask app以下是我的 flask 应用程序的配置

from flask import Flask
from flask_caching import Cache

app = Flask(__name__)

APICache = Cache(config={'CACHE_TYPE': 'filesystem','CACHE_DIR': "/cache"})
APICache.init_app(app)

How do I list all the key_prefix of all the cached data that has been stored until now?如何列出到目前为止已经存储的所有缓存数据的所有key_prefix

In the background, Flask-Caching uses cachelib , viewing how the cache system is implemented, filesystem doesn't have a dict or a way to access every key, the only way would be to list the directory, and call the get method for every file, like this:在后台,Flask-Caching 使用cachelib ,查看缓存系统是如何实现的,文件系统没有 dict 或访问每个键的方法,唯一的方法是列出目录,并为每个调用get方法文件,像这样:

for p in cache.cache._list_dir():
    k = os.path.split(p)[-1] # Getting just the key
    print(k, cache.get(k))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM