Note that there are some explanatory texts on larger screens.

plurals
  1. POCaching queries in Django
    primarykey
    data
    text
    <p>In a django project I only need to cache a few queries, using, because of server limitations, a cache table instead of memcached.</p> <p>One of those queries looks like this:</p> <p>Let's say I have a <code>Parent</code> object, which has a lot of <code>Child</code> objects. I need to store the result of the simple query <code>parent.childs.all()</code>.</p> <p>I have no problem with that, and everything works as expected with some code like</p> <pre><code>key = "%s_children" %(parent.name) value = cache.get(key) if value is None: cache.set(key, parent.children.all(), CACHE_TIMEOUT) value = cache.get(key) </code></pre> <p>But sometimes, just sometimes, the <code>cache.set</code> does nothing, and, after executing <code>cache.set</code>, <code>cache.get(key)</code> keeps returning <code>None</code>.</p> <p>After some test, I've noticed that <code>cache.set</code> is not working when <code>parent.children.all().count()</code> has higher values.<br /> That means that if I'm storing inside of <code>key</code> (for example) 600 children objects, it works fine, <br /> but it wont work with 1200 children.</p> <p>So <strong>my question is</strong>: <em>is there a limit to the data that a key could store? How can I override it?</em></p> <p><br /> <hr /> <br /> <strong>Second question:</strong> which way is "better", the above code, or the following one?</p> <pre><code>key = "%s_children" %(parent.name) value = cache.get(key) if value is None: value = parent.children.all() cache.set(key, value, CACHE_TIMEOUT) </code></pre> <p>The second version won't cause errors if <code>cache.set</code> doesn't work, so it could be a workaround to my issue, but <em>obviously</em> not a solution.</p> <p>In general, let's forget about my issue, which version would you consider "better"?</p>
    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.
 

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