List all the cached keys in Flask Cache

948 Views Asked by At

I have written an appliacation using Flask, and am caching the response of various api calls. following is the configuration of my flask app

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 ?

1

There are 1 best solutions below

0
On

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:

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