cache.py 2.19 KB
Newer Older
1 2 3 4
"""
This module aims to give a little more fine-tuned control of caching and cache
invalidation. Import these instead of django.core.cache.

5
Note that 'default' is being preserved for user session caching, which we're
6 7
not migrating so as not to inconvenience users by logging them all out.
"""
8 9
from functools import wraps

10 11
from django.core import cache

12

13 14
# If we can't find a 'general' CACHE defined in settings.py, we simply fall back
# to returning the default cache. This will happen with dev machines.
15 16
from django.utils.translation import get_language

17 18
try:
    cache = cache.get_cache('general')
19
except Exception:
20 21
    cache = cache.cache

22

23 24 25
def cache_if_anonymous(view_func):
    """
    Many of the pages in edX are identical when the user is not logged
26 27 28
    in, but should not be cached when the user is logged in (because
    of the navigation bar at the top with the username).

29 30 31 32
    The django middleware cache does not handle this correctly, because
    we access the session to put the csrf token in the header. This adds
    the cookie to the vary header, and so every page is cached seperately
    for each user (because each user has a different csrf token).
33

34 35 36
    Note that this decorator should only be used on views that do not
    contain the csrftoken within the html. The csrf token can be included
    in the header by ordering the decorators as such:
37

38 39 40 41
    @ensure_csrftoken
    @cache_if_anonymous
    def myView(request):
    """
42

43 44 45 46
    @wraps(view_func)
    def _decorated(request, *args, **kwargs):
        if not request.user.is_authenticated():
            #Use the cache
47 48 49
            # same view accessed through different domain names may
            # return different things, so include the domain name in the key.
            domain = str(request.META.get('HTTP_HOST')) + '.'
50
            cache_key = domain + "cache_if_anonymous." + get_language() + '.' + request.path
51 52 53 54
            response = cache.get(cache_key)
            if not response:
                response = view_func(request, *args, **kwargs)
                cache.set(cache_key, response, 60 * 3)
55

56
            return response
57

58 59 60
        else:
            #Don't use the cache
            return view_func(request, *args, **kwargs)
61

62
    return _decorated