Opened 5 years ago

Closed 4 years ago

#18191 closed New feature (fixed)

Don't consider Accept-Language when generating a cache key

Reported by: choongmin@… Owned by: Łukasz Langa
Component: Core (Cache system) Version: master
Severity: Normal Keywords: cache vary i18n
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no


Currently, Django makes different cache keys for different Accept-Language headers. But different Accept-Language headers may be resolved into the same language. For example, see the following Accept-Language headers collected in my site for last 30 minutes:

  • en-us
  • en-US
  • en-US,en;q=0.8
  • en-US,en;q=0.8,ko;q=0.6
  • fr-fr
  • ja-JP
  • ko
  • ko, en
  • ko-kr
  • ko-KR
  • ko-KR, en-US
  • ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3
  • ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
  • ko;q=1.0,en;q=0.5

It makes the cache system inefficient for multilingual sites. Since there is utils.cache._i18n_cache_key_suffix, it is unnecessary and redundant to include Accept-Language headers in a cache key. We should ignore them in utils.cache.learn_cache_key.

Note that not adding "Vary: Accept-Language" from the first place in middleware.locale.process_response is not a good solution because of proxy servers.

The attached patch removes Accept-Language from headerlist before generating a cache key.

If the patch is rejected, users like me should at least be able to ignore them.

Attachments (1)

ignore_accept_language.diff (851 bytes) - added by choongmin@… 5 years ago.

Download all attachments as: .zip

Change History (6)

Changed 5 years ago by choongmin@…

Attachment: ignore_accept_language.diff added

comment:1 Changed 4 years ago by Jannis Leidel

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 4 years ago by Jannis Leidel

Needs documentation: set
Needs tests: set
Patch needs improvement: set

comment:3 Changed 4 years ago by Łukasz Langa

Owner: changed from nobody to Łukasz Langa
Status: newassigned

comment:4 Changed 4 years ago by Łukasz Langa

Pull request:


comment:5 Changed 4 years ago by Carl Meyer <carl@…>

Resolution: fixed
Status: assignedclosed

In 6a057e1595dfe45a0411c116f603801266c87395:

Fixed #18191 -- Don't consider Accept-Language redundantly in cache key.

Thanks to choongmin for the original patch.

Note: See TracTickets for help on using tickets.
Back to Top