How do disable cache on urlfetch?

811 views
Skip to first unread message

lenza.m...@gmail.com

unread,
Mar 2, 2009, 6:47:58 PM3/2/09
to Google App Engine
My app needs to get a fresh copy of a dynamic webpage page every few
minutes. I am fetching date from a URL with this function:

def getUrlData(url):
result = urlfetch.fetch(url, headers = {'Cache-Control' : 'no-
cache', 'Pragma' : 'no-cache'})
if(result.status_code != 200):
logging.error( "Error getting URL data: %d" %
result.status_code)
return None
logging.warn("Fetch %s response headers: %s" % (url, repr
(result.headers)))
return result.content

However, it seems like I am still getting cached data. The logging
statement output the following:

Fetch [URL] response headers: {'Content-Length': '21883', 'X-Google-
Cache-Control': 'remote-cache-hit', 'Age': ' 633', 'Expires': 'Mon,
02 Mar 2009 23:49:23 GMT', 'Vary': 'Accept-Encoding', 'Server':
'Apache', 'last-modified': 'Mon, 02 Mar 2009 23:34:23 GMT',
'Connection': 'close', 'Via': 'HTTP/1.1 GWA (remote cache hit)',
'Cache-control': 'public; max-age=600', 'Date': 'Mon, 02 Mar 2009
23:34:23 GMT', 'Content-Type': 'text/html; charset=ISO-8859-1'}

Does anyone know how to ensure a fresh page?

admin go2

unread,
Mar 2, 2009, 9:41:01 PM3/2/09
to google-a...@googlegroups.com
You can add a random number after the url. For instance,
 
url=url+'?'+ random.random()
 
Good luck.
 
 
 
>>

--~--~---------~--~----~------------~-------~--~----~

lenza

unread,
Mar 2, 2009, 11:42:18 PM3/2/09
to Google App Engine
Turns out that this issue is in the issue tracker:

http://code.google.com/p/googleappengine/issues/detail?id=739

The answer is to make your request in the following manner:

result = urlfetch.fetch(url, headers = {'Cache-Control' : 'max-
age=300'})

Where "max-age" is the oldest you want data returned from the cache to
be.
Reply all
Reply to author
Forward
0 new messages