I just wonder how and where the response is stored when using Flask-Caching.
For example:
from flask import Flask, request
from flask_caching import Cache
import datetime
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
def make_cache_key(*args, **kwargs):
return request.url
@app.route('/', methods=['GET'])
@cache.cached(timeout=50, key_prefix=make_cache_key)
def foo():
time = str(datetime.datetime.now()) + " " + str(request.url)
return time, 200
if __name__ == '__main__':
app.run(debug=True)
Flask-Caching is an extension to Flask that adds caching support for various backends to any Flask application. By running on top of cachelib it supports all of werkzeug's original caching backends through a uniformed API. It is also possible to develop your own caching backend by subclassing flask_caching. backends.
The end result is it will allow you to cache your static files forever with a web server such as nginx. It also gzips your files at maximum compression. When you combine both things together it will save you money on hosting and make a very noticeable improvement on page load speeds for your users.
Flask itself does not provide caching for you, but Flask-Caching, an extension for Flask does.
In your example, it'll be stored in-memory of the Python interpreter.
Your setup is in-memory, so it won't scale between multiple servers. However, you have the option to specify a different cache backend (memcached or Redis, for example, or even your own custom one by extending the base cache class).
According to the docs we see that it uses werkzeug:
Besides providing support for all of werkzeug‘s supported caching backends through a uniformed API
Then when you look at the werkzeug cache docs:
If you are using the development server you can create a SimpleCache object, that one is a simple cache that keeps the item stored in the memory of the Python interpreter.
And then it goes on to show an example using your same setup ({'CACHE_TYPE': 'simple'}
), which it says is in-memory of the Python interpreter.
If you wanna use a different cache backend, look at Configuring Flask Caching:
Built-in cache types:
null: NullCache (default)
simple: SimpleCache
memcached: MemcachedCache (pylibmc or memcache required)
gaememcached: GAEMemcachedCache
redis: RedisCache (Werkzeug 0.7 required)
filesystem: FileSystemCache
saslmemcached: SASLMemcachedCache (pylibmc required)
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