Ticket #4992: cache_by_request_full_path.diff

File cache_by_request_full_path.diff, 3.9 KB (added by PeterKz, 7 years ago)

Cache by request.get_full_path() instead of request.path

  • django/utils/cache.py

     
    144144        if value is not None:
    145145            ctx.update(value)
    146146    return 'views.decorators.cache.cache_page.%s.%s.%s' % (
    147                key_prefix, iri_to_uri(request.path), ctx.hexdigest())
     147               key_prefix, iri_to_uri(request.get_full_path()), ctx.hexdigest())
    148148
    149149def get_cache_key(request, key_prefix=None):
    150150    """
    151     Returns a cache key based on the request path. It can be used in the
     151    Returns a cache key based on the request path and query. It can be used in the
    152152    request phase because it pulls the list of headers to take into account
    153153    from the global path registry and uses those to build a cache key to check
    154154    against.
     
    159159    if key_prefix is None:
    160160        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    161161    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
    162                     key_prefix, iri_to_uri(request.path))
     162                    key_prefix, iri_to_uri(request.get_full_path()))
    163163    headerlist = cache.get(cache_key, None)
    164164    if headerlist is not None:
    165165        return _generate_cache_key(request, headerlist, key_prefix)
     
    184184    if cache_timeout is None:
    185185        cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
    186186    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
    187                     key_prefix, iri_to_uri(request.path))
     187                    key_prefix, iri_to_uri(request.get_full_path()))
    188188    if response.has_header('Vary'):
    189189        headerlist = ['HTTP_'+header.upper().replace('-', '_')
    190190                      for header in cc_delim_re.split(response['Vary'])]
     
    192192        return _generate_cache_key(request, headerlist, key_prefix)
    193193    else:
    194194        # if there is no Vary header, we still need a cache key
    195         # for the request.path
     195        # for the request.get_full_path()
    196196        cache.set(cache_key, [], cache_timeout)
    197197        return _generate_cache_key(request, [], key_prefix)
    198198
  • django/middleware/cache.py

     
    2323
    2424More details about how the caching works:
    2525
    26 * Only parameter-less GET or HEAD-requests with status code 200 are cached.
     26* Only GET or HEAD-requests with status code 200 are cached.
    2727
    2828* The number of seconds each page is stored for is set by the "max-age" section
    2929  of the response's "Cache-Control" header, falling back to the
     
    115115        if self.cache_anonymous_only:
    116116            assert hasattr(request, 'user'), "The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware."
    117117
    118         if not request.method in ('GET', 'HEAD') or request.GET:
     118        if not request.method in ('GET', 'HEAD'):
    119119            request._cache_update_cache = False
    120120            return None # Don't bother checking the cache.
    121121
  • docs/topics/cache.txt

     
    487487said to "vary on language."
    488488
    489489By default, Django's cache system creates its cache keys using the requested
    490 path -- e.g., ``"/stories/2005/jun/23/bank_robbed/"``. This means every request
    491 to that URL will use the same cached version, regardless of user-agent
     490path and query -- e.g., ``"/stories/2005/?order_by=author"``. This means every
     491request to that URL will use the same cached version, regardless of user-agent
    492492differences such as cookies or language preferences.
    493493
    494494That's where ``Vary`` comes in.
Back to Top