0

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 ?

phileinSophos
  • 362
  • 4
  • 22
  • Does this answer your question? [How can I retrieve all keys from a flask cache?](https://stackoverflow.com/questions/60118783/how-can-i-retrieve-all-keys-from-a-flask-cache) – v25 Apr 21 '21 at 22:04
  • 1
    No, `cache.cache._cache` dosen't work for 'CACHE_TYPE': 'filesystem' – phileinSophos Apr 22 '21 at 04:03

1 Answers1

0

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))
David Davó
  • 417
  • 1
  • 4
  • 18