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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With