Flask-caching doesn't have any mechanism to delete cache/expired cache automatically.
But in Simple
and FileSystemCache
mode, it have a CACHE_THRESHOLD
config setting, if the number of cache more than threshold
setting, it will delete all expired cache and every cache that index divisible by three.
Flask-caching Simple
mode source code (Version 1.10.1):
def _prune(self):
if len(self._cache) > self._threshold:
now = time()
toremove = []
for idx, (key, (expires, _)) in enumerate(self._cache.items()):
if (expires != 0 and expires <= now) or idx % 3 == 0:
toremove.append(key)
for key in toremove:
self._cache.pop(key, None)
logger.debug("evicted %d key(s): %r", len(toremove), toremove)
Also, CACHE_THRESHOLD
can be setting before initializing
from flask import Flask
from flask_caching import Cache
config = {
"CACHE_TYPE": "SimpleCache",
"CACHE_THRESHOLD": 300 # It can be setting before initializing
}
app = Flask(__name__)
app.config.from_mapping(config)
cache = Cache(app)
By and large, if you use these mode, you can write the schedule jobs to delete expired cache like above source code.