Opened 14 years ago

Closed 13 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.

Change History (6)

comment:1 by anonymous, 14 years ago

Needs tests: set

comment:2 by anonymous, 14 years ago

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

comment:3 by Luke Plant, 14 years ago

Resolution: needsinfo
Severity: Normal
Status: newclosed
Type: 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 by Chris Beaven, 14 years ago

Type: UncategorizedBug

comment:5 by anonymous, 13 years ago

Easy pickings: unset
Resolution: needsinfo
Status: closedreopened
UI/UX: unset

comment:6 by anonymous, 13 years ago

Resolution: duplicate
Status: reopenedclosed

This is a duplicate of ticket #9249.

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