Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you configure beaker to save to the filesystem, you can easily see that each argument is being pickled as well. Example:</p> <pre><code>tp3 sS'tags &lt;myapp.controllers.tags.TagsController object at 0x103363c10&gt; &lt;MySQLdb.cursors.Cursor object at 0x103363dd0&gt; apple' p4 </code></pre> <p>Notice that the cache "key" contains more than just my keyword, "apple," but instance-specific information. This is pretty bad, because especially the 'self' won't be the same across invocations. The cache will result in a miss every single time (and will get filled up with useless keys.)</p> <p>The method with the cache annotation should <strong>only</strong> have the arguments to correspond to whatever "key" you have in mind. To paraphrase this, let's say that you want to store the fact that "John" corresponds to value 555-1212 and you want to cache this. Your function should not take anything except a string as an argument. Any arguments you pass in should stay constant from invocation to invocation, so something like "self" would be bad.</p> <p>One easy way to make this work is to <em>inline</em> the function so that you don't need to pass anything else beyond the key. For example:</p> <pre><code>def index(self): # some code here # suppose 'place' is a string that you're using as a key. maybe # you're caching a description for cities and 'place' would be "New York" # in one instance @cache_region('long_term', 'place_desc') def getDescriptionForPlace(place): # perform expensive operation here description = ... return description # this will either fetch the data or just load it from the cache description = getDescriptionForPlace(place) </code></pre> <p>Your cache file should resemble the following. Notice that only 'place_desc' and 'John' were saved as a key.</p> <pre><code>tp3 sS'place_desc John' p4 </code></pre>
 

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