Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Flask Caching filesystem cache shared across processes?

Let us assume I use Flask with the filesystem cache in combination with uWSGI or gunicorn, either of them starting multiple processes or workers. Do all these processes share the same cache? Or asked differently, do the functions and parameters always evaluate to the same cache key regardless of process pid, thread state etc.?

For instance, consider the following minimal example:

import time

from flask import Flask, jsonify
from flask_caching import Cache

app = Flask(__name__)

cache = Cache(app, config={
    'CACHE_TYPE': 'filesystem',
    'CACHE_DIR': 'my_cache_directory',
    'CACHE_DEFAULT_TIMEOUT': 3600,
})


@cache.memoize()
def compute(param):
    time.sleep(5)
    return param + 1


@app.route('/')
@app.route('/<int:param>')
def main(param=41):
    expensive = compute(param)
    return jsonify({"Hello expensive": expensive})


if __name__ == '__main__':
    app.run()

Will www.example.com/41 just take 5 seconds once and then (for 3600 seconds) be available instantly regardless of the uWSGI or gunicorn workers?

like image 270
SmCaterpillar Avatar asked Oct 17 '22 06:10

SmCaterpillar


1 Answers

If i run it locally on my machine, the cache is stable across different processes as well as even different restarts of the entire server.

like image 84
SmCaterpillar Avatar answered Oct 20 '22 10:10

SmCaterpillar