Django: Ticket #5815: Adds per-view cache refreshing (clearing)https://code.djangoproject.com/ticket/5815
<p>
This patch adds the possibility to clear(refresh) cached data per view.
</p>
<p>
SmileyChris sugested it would be good to add another key to the cached keys where we could keep track of all the different cache_page_key hashes, so we that can delete those then.
</p>
<p>
It is possible to clear the cache for an specific view, to achieve that, you
should tell your view's url path (and optionally, a key_prefix) to
<code></code>clear_cache_for_path(path, key_prefix=None)<code></code>.
</p>
<pre class="wiki"> from django.utils.cache import clear_cache_for_path
# clear the all cached data for path '/blog/posts/2/'
clear_cache_for_path('/blog/posts/2/')
</pre><p>
Note that it will delete every page cached from matching this path, <code></code>Vary<code></code> headers
doesn't matter.
</p>
<p>
NOTE: Thanks SmileyChris for your help and supervision with this.
</p>
en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/5815
Trac 1.2k0001Thu, 25 Oct 2007 05:22:24 GMTattachment sethttps://code.djangoproject.com/ticket/5815
https://code.djangoproject.com/ticket/5815
<ul>
<li><strong>attachment</strong>
set to <em>add_cache_clear_feature.diff</em>
</li>
</ul>
<p>
adds per-view clear cache support
</p>
TicketSimon G.Thu, 25 Oct 2007 09:37:02 GMTstage changedhttps://code.djangoproject.com/ticket/5815#comment:1
https://code.djangoproject.com/ticket/5815#comment:1
<ul>
<li><strong>stage</strong>
changed from <em>Unreviewed</em> to <em>Design decision needed</em>
</li>
</ul>
TicketJacobTue, 04 Dec 2007 14:48:51 GMTneeds_better_patch, needs_tests, stage changedhttps://code.djangoproject.com/ticket/5815#comment:2
https://code.djangoproject.com/ticket/5815#comment:2
<ul>
<li><strong>needs_better_patch</strong>
set
</li>
<li><strong>needs_tests</strong>
set
</li>
<li><strong>stage</strong>
changed from <em>Design decision needed</em> to <em>Accepted</em>
</li>
</ul>
<p>
I like the idea. However, I'm -1 on the implementation -- adding another key to the cache seems unnecessary.
</p>
TicketChris BeavenThu, 21 Feb 2008 21:25:54 GMThttps://code.djangoproject.com/ticket/5815#comment:3
https://code.djangoproject.com/ticket/5815#comment:3
<p>
I don't see how we can get around adding a key to index the view's cache keys. Since they are hashed, there's no way of retrieving them with the current cache API (you'd need some type of <code>.get_starts_with()</code> method which I'm not sure would be even possible)
</p>
TicketJulian BezMon, 18 Aug 2008 11:28:18 GMThttps://code.djangoproject.com/ticket/5815#comment:4
https://code.djangoproject.com/ticket/5815#comment:4
<p>
One could argue that you are doing something wrong if you need per-site or per-view cache clearing.
</p>
<p>
If you have a page that changes more than once in a month, you are better off using the cache template tag. This allows you to share cached content between views and the keys can be deleted more easily when something got updated. With per-view caching you always cache the whole page, for every page. Imagine how often you cache your header or footer that way if you have a larger site.
</p>
TicketGabriel HurleyFri, 01 Apr 2011 18:00:52 GMTtype, severity sethttps://code.djangoproject.com/ticket/5815#comment:5
https://code.djangoproject.com/ticket/5815#comment:5
<ul>
<li><strong>type</strong>
set to <em>New feature</em>
</li>
<li><strong>severity</strong>
set to <em>Normal</em>
</li>
</ul>
TicketAymeric AugustinSat, 18 Feb 2012 10:39:49 GMTui_ux sethttps://code.djangoproject.com/ticket/5815#comment:6
https://code.djangoproject.com/ticket/5815#comment:6
<ul>
<li><strong>ui_ux</strong>
unset
</li>
</ul>
<p>
Change UI/UX from NULL to False.
</p>
TicketAymeric AugustinSat, 18 Feb 2012 10:45:09 GMTeasy sethttps://code.djangoproject.com/ticket/5815#comment:7
https://code.djangoproject.com/ticket/5815#comment:7
<ul>
<li><strong>easy</strong>
unset
</li>
</ul>
<p>
Change Easy pickings from NULL to False.
</p>
TicketAymeric AugustinSun, 24 Feb 2013 11:26:49 GMTstage changedhttps://code.djangoproject.com/ticket/5815#comment:8
https://code.djangoproject.com/ticket/5815#comment:8
<ul>
<li><strong>stage</strong>
changed from <em>Accepted</em> to <em>Design decision needed</em>
</li>
</ul>
<p>
Setting to DDN since there's some disagreement between Jacob and Chris.
</p>
<p>
Explicit cache invalidation can be useful in some scenarios, it'd be nice to support this.
</p>
TicketJacobSun, 24 Feb 2013 18:14:49 GMTstage changedhttps://code.djangoproject.com/ticket/5815#comment:9
https://code.djangoproject.com/ticket/5815#comment:9
<ul>
<li><strong>stage</strong>
changed from <em>Design decision needed</em> to <em>Accepted</em>
</li>
</ul>
<p>
If there's really not a better implementation (perhaps there isn't) then I'm OK with this going in as-is.
</p>
Ticket