For example like this, is it necessary to use key_prefix
?
@cache.cached(timeout=50, key_prefix='all_comments')
def get_all_comments():
comments = do_serious_dbio()
return [x.author for x in comments]
cached_comments = get_all_comments()
In the document, it says that the key_prefix
's default value is request.path cache_key.:
, what does cache_key
mean, how can I use it? What does key_prefix
do?
Simply set your app. config's CACHE_TYPE key to "null" before you initialize Flask-Cache: app. config["CACHE_TYPE"] = "null" # change to "redis" and restart to cache again # some time later cache.
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.
By default, we have null, which means that there will be no cache until and unless specified. There are various other types of cache present and the most widely used is the simple cache method and other than that one may use as per the desirability of the app that is being developed using flask.
First the request.path
is everything (except params) after your script_root
. For example:
For a url like, http://127.0.0.1:5000/users/login/
, request data is:
request.path is: /users/login/
For a url like in the example from the link above, http://www.example.com/myapplication/page.html?x=y
, request data is:
request.path is: /page.html
Q. What does cache_key mean, how can I use it?
The cache_key
is the key that is used to access the particular cached value. As you know that the cache is a key-value store.
In Flask-Cache the cache_key
is generated by the extension and we are not supposed to use it ourselves.
Q. What does key_prefix do?
key_prefix
is used to generate the cache_key
for a cached value. See make_cache_key
source to see how exactly it is done.
Q. Is it necessary to use key_prefix?
Let's say you are calling get_all_comments
from 2 different view functions, say manage()
, and view()
. And you don't specify a key_prefix
, while caching get_all_comments
with @cached
.
The first time you view a post through view
the get_all_comments
output is cached with a default key like: view/view
or view/module/view
, or whatever the value of view/%s
is, where %s
is request.path
.
Next when you manage a post through manage
, the get_all_comments
output is not read from the cache, since the cache_key
applied to get data from the cache has changed to view/manage
and is not the old view/view
, because the request.path has now changed.
The whole point of caching get_all_comments
here was to get the data from the cache whenever possible and not the db, but since the keys have changed between view functions the data is actually retrieved both the times from the db itself.
However in case you had specified a key_prefix
like all_comments
, then the first time data is retrieved from the db, and the next time the cache_key
is still all_comments
and value is found, and data is accessed from the cache instead of db.
So when you have cases like the above it is obviously better to use a key_prefix
, in other cases when the function is always called from a single path/ view function then it is ok to let the default one be used.
Note: The cache_key is generated/calculated for every request, see the source:
cache_key = decorated_function.make_cache_key(*args, **kwargs)
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