Search code examples
pythoncachingflaskflask-caching

Flask-Caching FileSystemCache method does not delete cache items upon timeout


I have a Flask app, and I have implemented the "Flask-Caching" extension. I am using the "FileSystemCache" method. This is all new to me so it may be working properly and I'm unaware.

I have found that when I call cache.clear(), I will see items delete from the directory specified as my cache location. However, when I set the timeout to be something really short, I do not see the files delete when the timeout duration is reached.

I'm not sure if it is supposed to delete or if I should write a background task to delete all files older than the timeout setting. Each file is small but they accumulate very quickly.

I ask that someone advise me if this is working as intended. Creating a background task to clear the directory is no issue but it seems like this should be happening automatically.

In terms of relevant code, there isn't much:

cache = Cache(app,config={'CACHE_TYPE': 'filesystem', 
                      'CACHE_DIR': r"<my cache directory>", 
                      'CACHE_DEFAULT_TIMEOUT': 15})

The timeout is only 15 seconds to aid my testing here but it will be increased later. Throughout my code, I'm only really using @cache.memoize() and the occasional cache.delete_memoized().


Solution

  • Flask-Caching itself does not necessarily automatically delete cache items after they time out. Generally, it will only do so when performing certain operations. For example, in the FileSystemCache, if you look at the source code for the get() function you can see that if you try to get a cached item that has expired, it deletes the file and returns None.

    Also, looking at the source code for the set() function, you can see the call to _prune() (source code). This is an internal function which (if the number of cached files exceeds the threshold set in the constructor) will go through the files in the cache directory and delete the expired files.

    The reasons you are not seeing any files be deleted may be because even if the timeout is low, the threshold may be high enough that you aren't caching enough files for it to start deleting some. You can use the CACHE_THRESHOLD config variable to set the maximum number of files that can be cached.

    I hope this helps!