Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First the <a href="http://flask.pocoo.org/docs/api/#flask.Request.path" rel="noreferrer" title="http://flask.pocoo.org/docs/api/#flask.Request.path"><code>request.path</code></a> is everything (except params) after your <code>script_root</code>. For example:</p> <ol> <li><p>For a url like, <code>http://127.0.0.1:5000/users/login/</code>, request data is:</p> <pre><code>request.path is: /users/login/ </code></pre></li> <li><p>For a url like in the example from the link above, <code>http://www.example.com/myapplication/page.html?x=y</code>, request data is:</p> <pre><code>request.path is: /page.html </code></pre></li> </ol> <hr> <blockquote> <p>Q. What does cache_key mean, how can I use it?</p> </blockquote> <p>The <code>cache_key</code> is the key that is used to access the particular cached value. As you know that the cache is a key-value store.</p> <p>In Flask-Cache the <code>cache_key</code> is generated by the extension and we are not supposed to use it ourselves.</p> <hr> <blockquote> <p>Q. What does key_prefix do?</p> </blockquote> <p><code>key_prefix</code> is used to generate the <code>cache_key</code> for a cached value. See <a href="https://github.com/thadeusb/flask-cache/blob/master/flask_cache/__init__.py#L217" rel="noreferrer" title="https://github.com/thadeusb/flask-cache/blob/master/flask_cache/__init__.py#L217"><code>make_cache_key</code> source</a> to see how exactly it is done. </p> <hr> <blockquote> <p>Q. Is it necessary to use key_prefix?</p> </blockquote> <p>Let's say you are calling <code>get_all_comments</code> from 2 different view functions, say <code>manage()</code>, and <code>view()</code>. And you don't specify a <code>key_prefix</code>, while caching <code>get_all_comments</code> with <code>@cached</code>.</p> <p>The first time you <em>view a post</em> through <code>view</code> the <code>get_all_comments</code> output is cached with a <a href="http://packages.python.org/Flask-Cache/#flask.ext.cache.Cache.cached" rel="noreferrer" title="http://packages.python.org/Flask-Cache/#flask.ext.cache.Cache.cached">default key</a> like: <code>view/view</code> or <code>view/module/view</code>, or whatever the value of <code>view/%s</code> is, where <code>%s</code> is <code>request.path</code>.</p> <p>Next when you <em>manage a post</em> through <code>manage</code>, the <code>get_all_comments</code> output is not read from the cache, since the <code>cache_key</code> applied to get data from the cache has changed to <code>view/manage</code> and is not the old <code>view/view</code>, because the request.path has now changed.</p> <p>The whole point of caching <code>get_all_comments</code> 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.</p> <p>However in case you had specified a <code>key_prefix</code> like <code>all_comments</code>, then the first time data is retrieved from the db, and the next time the <code>cache_key</code> is still <code>all_comments</code> and value is found, and data is accessed from the cache instead of db.</p> <p>So when you have cases like the above it is obviously better to use a <code>key_prefix</code>, 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.</p> <hr> <p>Note: The cache_key is generated/calculated for every request, see the <a href="https://github.com/thadeusb/flask-cache/blob/master/flask_cache/__init__.py#L208" rel="noreferrer" title="https://github.com/thadeusb/flask-cache/blob/master/flask_cache/__init__.py#L208">source</a>:</p> <pre><code>cache_key = decorated_function.make_cache_key(*args, **kwargs) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload