Code

Opened 3 years ago

Closed 3 years ago

Last modified 2 years ago

#15978 closed Bug (worksforme)

adding host to cache_key to enable subdomain caching

Reported by: lpiatek@… Owned by: nobody
Component: Core (Cache system) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If we try to run django on wildcard domain (all subdomains pointing one django app instance) there is problem when we turn on caching system. Django do not add any modificator to cache_key_suffix for subdomains. We get the same content on main page for different domain.

There is function in django.utils.cache._i18n_cache_key_suffix which django use to create prefix. To get it working I simply added one line which adds request.get_host() to this prefix. Details here:

http://stackoverflow.com/questions/3973852/django-caching-multi-domain-subdomain-wildcard-problem/4147366#4147366

I don't know is it the best solution You can choose. I'm waiting for others to comment.

Patch:

*** django/utils/cache.py       2011-05-06 21:40:42.000000000 +0200
--- ../django/utils/cache.py    2011-05-06 21:18:33.000000000 +0200
***************
*** 151,156 ****
--- 151,157 ----
          # LANGUAGE_CODE to request, then fall back to the active language
          # which in turn can also fall back to settings.LANGUAGE_CODE
          cache_key += '.%s' % getattr(request, 'LANGUAGE_CODE', get_language())
+     cache_key += '.%s' % (request.get_host())
      return cache_key

  def _generate_cache_key(request, method, headerlist, key_prefix):

PS: I'm now considering whether request.get_host() can return unicode? If yes there should be an u before string i guess?

Attachments (1)

subdomain_caching.patch (554 bytes) - added by lpiatek@… 3 years ago.

Download all attachments as: .zip

Change History (5)

Changed 3 years ago by lpiatek@…

comment:1 Changed 3 years ago by aaugustin

  • Needs documentation set
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

This suggestion sounds reasonable, but there are at least two ways to implement it:

  • with request.get_host(), like your patch — but some people may want to serve the same content at foobar.com and www.foobar.com
  • with settings.SITE_ID

I think a discussion on django-developers would be useful to confirm this change doesn't introduce nasty side-effects and to determine the best implementation.

Last edited 3 years ago by aaugustin (previous) (diff)

comment:2 Changed 3 years ago by lpiatek

Moved discussion to Django Developers:
http://groups.google.com/group/django-developers/browse_frm/thread/59ed1262d7df4da0

Waiting for any django geek to take a look ...

comment:3 Changed 3 years ago by aaugustin

  • Resolution set to worksforme
  • Status changed from new to closed
  • UI/UX unset

In fact the proper solution is to set the KEY_PREFIX setting to something different for each site.

comment:4 Changed 2 years ago by lpiatek

I took another way of having middleware adding Vary headers, details here: http://stackoverflow.com/a/9701153/479931

I agree first idea was not so clever...

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.