1

I am using python 2.7.3 with varnish-3.0.2.

I am using python urllib2 library.

I wish to force the server to bring back the latest data from the DB and not to use the varnish cache so I added the following to my request

    req.add_header('Pragma', 'no-cache');
    req.add_header('Cache-Control', 'no-cache');

But this does not work - it has no effect.

Here is a code snippet:

req = urllib2.Request(url='http://%s:%d/api/myquery/myrequest' % (self.host, self.port))
return self.httpreq(req)
    .
    .

def httpreq(self, req, data=None):

    req.add_header('Pragma', 'no-cache');
    req.add_header('Cache-Control', 'no-cache');

    try:
        http_handler = urllib2.HTTPHandler(debuglevel=1)
        opener = urllib2.build_opener(http_handler, urllib2.HTTPCookieProcessor(self.cj))
        if data:
            req = opener.open(req, data, timeout=wwbsettings.HTTP_TIMEOUT)
        else:
            req = opener.open(req, timeout=wwbsettings.HTTP_TIMEOUT)
        resp = req.read()
        log('data=%s' % resp)
        content = json.loads(resp)
        return content
    except urllib2.HTTPError as e:
        resp = e.read()
        log('HTTPError: %s - %s' % (e, resp))
        content = json.loads(resp)
        return content
    except urllib2.URLError as e:
        log('URLError: %s' % e)
        return None
    except Exception as e:
        log('Exceptions: %s' % e)
        return None

And here is the request header:

header: Server: nginx/1.1.19
header: Date: Mon, 12 Jan 2015 17:51:22 GMT
header: Content-Type: application/json
header: Content-Length: 636
header: Connection: close
header: Vary: Accept
header: ETag: "acfa981667dfb6de47881623ad092672"
header: Allow: HEAD, GET, OPTIONS
header: Cache-Control: s-maxage=604800, public, max-age=300
header: Accept-Ranges: bytes
header: X-Varnish: 1115442413
header: Age: 0
header: Via: 1.1 varnish
header: X-Varnish-Cache: MISS

I do not receive the latest data in my response - can anyone help here? Much appreciated.

user3350887
  • 161
  • 2
  • 11
  • 1
    You might want to have a look at this stackoverflow question and user Rudd-O's answer: http://stackoverflow.com/questions/9009966/how-to-set-varnish-cache-control-headers – Joe Young Jan 12 '15 at 18:05
  • thanks - so using 'no-cache' with varnish is not an option – user3350887 Jan 12 '15 at 18:32
  • header: X-Varnish-Cache: MISS would indicate that varnish did NOT returned a cached version from its memory but instead retreived a fresh version from the webserver. What does the webserver log indicate? – Marcel Dumont Jan 12 '15 at 20:45
  • You are correct - the request does miss the cache and hits the server - which is what I want. – user3350887 Jan 13 '15 at 11:14
  • My issue was that the request was internally referencing another table which was itself cached for 60secs... – user3350887 Jan 13 '15 at 11:15

0 Answers0