I'm attempting to implement a file-based per-view cache on some specific report pages. I'll focus on one in particular as an example, called ScorecardSummary.
In urls.py:
url(r'^(?P<institution_slug>[^/]+)/report/(?P<submissionset>[^/]+)/$',
ScorecardSummary.as_view(), name='scorecard-summary'),
In views.py:
class ScorecardSummary(ScorecardView):
template_name = 'institutions/scorecards/summary.html'
To implement the caching, I altered the view like so:
class ScorecardSummary(ScorecardView):
template_name = 'institutions/scorecards/summary.html'
@method_decorator(vary_on_cookie)
@method_decorator(cache_page(86400 * 30, cache="filecache"))
def dispatch(self, request, *args, **kwargs):
if request.user.is_anonymous():
response = super(ScorecardSummary, self).dispatch(request, *args, **kwargs)
patch_cache_control(response, public=True)
else:
response = super(ScorecardSummary, self).dispatch(request, *args, **kwargs)
patch_cache_control(response, private=True)
return response
I've tried a few different permutations of these decorators both before the method and within the logic using patch_cache_control() or patch_vary_headers().
Without vary_on_cookie, every user sees the original user's information at the top corner of the page (i.e. "email address | Log Out Link"). This is also true of anonymous users, and this is where this problem began.
I found the above solution. As written, this works in that it creates a new cached version of the page for each user or anon. This is fantastic for each
My problem is that EVERY time an anon accesses the page, regardless of an existing cache file, it will create a new cache rather than loading the existing one, which sort of defeats the purpose. If I fix it to cache for anonymous, then we're back to EVERY user being served the exact same version and I'm right back where I started.
I've also tried using vary_on_headers('User-Agent'), but it still differentiates every anonymous user rather than treating them as identical. Setting private or public to True or not seems to have no effect.
So my question is this:
Is there some permutation of these decorators to control the caching such that BOTH authenticated and anonymous are cached, and if an anonymous user has already triggered the creation of a cache, that version is served to all anons rather than always missing.
Alternatively, is there a better way to implement this per-view caching that would bypass this issue and accomplish the same end? (i.e. caching regardless of authentication but without sharing user-specific data).
Thanks!