Code

Opened 3 years ago

Closed 3 years ago

#15626 closed Bug (duplicate)

Cache middleware doesnt work with pages contain google analytics script

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

Description

How it appears:
Every request page is taken from cache (cache hits rate increasing), but then it is generated again and put into cache what causes growth of memory occupied by cache.

How to reproduce:

  1. Setup cache settings:
CACHE_BACKEND = 'locmem://'
# or
CACHE_BACKEND = 'memcached://localhost:9450'

CACHE_MIDDLEWARE_SECONDS = 60

MIDDLEWARE_CLASSES = (
	'django.middleware.cache.UpdateCacheMiddleware',
	'django.middleware.common.CommonMiddleware',
	'django.contrib.sessions.middleware.SessionMiddleware',
 	'django.middleware.csrf.CsrfViewMiddleware',
	'django.middleware.http.ConditionalGetMiddleware',
	'django.middleware.gzip.GZipMiddleware', 
	'django.contrib.auth.middleware.AuthenticationMiddleware',
	'django.contrib.messages.middleware.MessageMiddleware',
	'django.middleware.cache.FetchFromCacheMiddleware',
)

now cache will work as expected
(First response updates cache backend and return 200 (ok), then 304 (not modified) every request during 1 minute)

  1. Now install Google analytics script into template.
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-12345678-9']);
  _gaq.push(['_setDomainName', '.domain.com']);
  _gaq.push(['_trackPageview']);

  (function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

Page must be open with browser that supports JS and local cache turned off.

Now every request hits cache, than view, database, updates cache and returns response.
in response http headers:

Etag is changing everytime (but content of page is not changed)
Last-Modified sets to current server time
Expires sets to current time + max age

Conditional requests If-Not-Modified-Since  and If-None-Match are always returning 200 OK response.

Next, if turn off JS in browser (for example install NoScript in Firefox) cache works fine, conditional requests get 304. Found this bug in Firefox, Opera, Chrome.

Attachments (0)

Change History (6)

comment:1 Changed 3 years ago by anonymous

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

comment:2 Changed 3 years ago by anonymous

I think this issue is related with another ticket: http://code.djangoproject.com/ticket/15538

comment:3 Changed 3 years ago by lukeplant

  • Resolution set to needsinfo
  • Severity set to Normal
  • Status changed from new to closed
  • Type set to Uncategorized

Django's cache middleware doesn't look at the content of your template, so something more complicated must be going on. If you are sure this is really happening, please put together a sample project that will allow us to quickly verify this behaviour. Thanks.

comment:4 Changed 3 years ago by SmileyChris

  • Type changed from Uncategorized to Bug

comment:5 Changed 3 years ago by anonymous

  • Easy pickings unset
  • Resolution needsinfo deleted
  • Status changed from closed to reopened
  • UI/UX unset

comment:6 Changed 3 years ago by anonymous

  • Resolution set to duplicate
  • Status changed from reopened to closed

This is a duplicate of ticket #9249.

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.