Black Lives Matter. Please consider donating to Black Girls Code today.

Do I need to clean the filesystem cache when using memoization?

Hi,

I’m testing using the filesystem cache to increase an app’s performance (as per the example here). The code I’m using is something like this:

from flask_caching import Cache
cache = Cache(app.server, config={
    'CACHE_TYPE': 'filesystem',
    'CACHE_DIR': 'cache-directory'
})
@cache.memoize(timeout=TIMEOUT)
def query_data():
    # This could be an expensive data querying step
    return data.to_json(date_format='iso', orient='split')

def data():
    return pd.read_json(query_data(), orient='split')

However, in my tests I’ve noticed that every time the timeout is hit and the query is done again a new version of the serialized file is generated in the cache directory, without the older one being removed.

Are these files deleted automatically after a set amount of time or do I need to clear them in the code (maybe adding cache.clean() somewhere in the memoized function) ? What I’m worried about is that eventually the cache would grow too big and take up all the space available in the server.

Thanks!

1 Like

See the options here: https://flask-caching.readthedocs.io/en/latest/

There is a clear cache function (cache.clear()) but you can also set the max number of items so that once the cache starts saving more than that number, the first items are deleted (CACHE_THRESHOLD)

2 Likes