Dream: python bindings for squidclient

11 October 2005   3 comments   This site, Python

Powered by Fusion×

At the moment I'm not running Squid for this site but if experimentation time permits I'll have it running again soon. One thing I feel uneasy about is how to "manually" purge cached pages that needs to be updated. For example, if you read this page (and it's cached for one hour) and post a comment, then I'd like to re-cache this page with a purge. Setting a HTTP header could be something but that I would only be able to do on the page where you have this in the URL:


which, because of the presence of a querystring, is not necessarily cached anyway. The effect is that as soon as the "?msg=Comment+added" is removed from the URL, the viewer will see the page as it was before she posted her comment. squidclient might be the solution. ...sort of.

squidclient is an executable program that you get when you install the squid cache server. As described in this documentation you can manually purge any cache on a site which would have the desired effect to the problem mentioned above. The only problem is that I have about 30-60 posted comments per day and that would be a hell of a lot of command line calls to squid. Secondly, they'd probably be quite slow and the person posting a comment won't be prepared to wait that long. The code for this would be something like this:

cmd = 'squidclient -m PURGE %s' % self.absolute_url()
result = os.popen4(cmd).read()
return result.find('200 OK') > -1

(obviously this would need to be wrapped in some security assertions)

An even better solution exists only as a dream. A Python binding for squidclient that I can use directly from my Zope Python code:

# <pseudocode>
import pysquidclient
server = pysquidclient.Server('localhost', 80)
r = server.purgeURL(self.absolute_url())
return r.isOK()
# </pseudocode>

Imagine that! You could create the server instance for the duration of the Zope server running just call the purgeURL() function multiple times thus saving looooads of time. I guess it might be worth testing the os.popen4() method to see if it works. If it doesn't work, then maybe it's time to start looking at ESI


Thanks Kevin (and Seb) for pointing this out. The solution is something like this:

(scheme, host, path, params, query, fragment
            ) = urlparse.urlparse(objecturl)
h = httplib.HTTPConnection(host)
h.request('PURGE', path)
r = h.getresponse()
return r.status, r.reason


Hej Peter!
Squidclient uses HTTP, so there are lots of python clients to choose from already.

I'm pretty sure that Zope supports automatic purging already. Look at the "Web Accelerator" (or something like that) product. I haven't touched it in a while.

Take a look at lib/python/Products/StandardCacheManagers/AcceleratedHTTPCacheManager.py::ZCache_invalidate for how to do it...


Peter Bengtsson
Thanks Kevin and Anon. See my UPDATE above.

Your email will never ever be published

Related posts

Best Image Replacement Technique guide 07 October 2005
table-layout: fixed 13 October 2005
Related by keywords:
Is Nginx obsolete now that we have Amazon CloudFront? 28 July 2012
A great thing about Squid: Calamaris 04 July 2008
Custom CacheMiddleware that tells Javascript a page is cached in Django 24 August 2009
ESI and Squid and Zope 17 December 2003
Keybinding ALT-F in Jed 27 January 2004
Squid now working in front of my Zope 30 July 2003