One of the nice properties of HTTP as a protocol is that it includes a very well-thought caching protocol, which allows for proxies and caches to serve as intermediary between the authoritative server and the end-user client.
While this is often used either in big proxies set up where one proxy caches results for a large set of clients, or at a micro-level where a given user agent keeps some set of resources in its private cache, I’ve found it much more useful to set up a full cache for my laptop that allows to do transparently off-line browsing.
What is more irritating than being off-line and not having access to all these pages you access so frequently? Why should I need to be connected to browser the W3C specifications, which, once they have reached the Recommendation status, pretty much don’t change at all?
There are several software packages that can be used as proxy cache, but I have found them to be more often faulty in the way they interact with HTTP than not. I’ve settled to use Squid for the past few months with really good results; by default, Squid is configured to handle the big proxy-cache use case rather than my offline browsing one. So here is my recipe to make Squid a more approriate choice for this use case:
you probably want some long time to live for the cached objects, so that they don’t get removed too quickly from your cache: to set the maximum time to live to one year, you need to set the last parameter of
refresh_patternto 525600; my own setting is:
refresh_pattern . 0 40% 525600
- you also need to say how big you want your cache to grow up to; I’ve set it to 500 Mo with
cache_dir ufs /var/spool/squid 500 16 256— I would have set a higher bar if my disk wasn’t getting so full already
- Squid has a special mechanism to be used as offline cache, which is activated by the squidclient tool: /usr/bin/squidclient mgr:offline_toggle
- by default, that command needs a password; to make this available by anyone on localhost without password, add the line cachemgr_passwd none offline_toggle in the configuration file
- to get Squid in this offline mode automatically when you lose your network connection, I’ve added in my
/etc/network/if-up.d/directory the following script (make sure to make it executable):
and similary in
#!/bin/sh # Quit if we're called for the loopback if [ "$IFACE" = lo ]; then exit 0 fi # make sure the state is OFF if [ -z "`/usr/bin/squidclient mgr:offline_toggle|grep OFF`" ] then /usr/bin/squidclient mgr:offline_toggle ; fi
#!/bin/sh # Quit if we're called for the loopback if [ "$IFACE" = lo ]; then exit 0 fi # make sure the state is ON if [ -z "`/usr/bin/squidclient mgr:offline_toggle|grep ON`" ] then /usr/bin/squidclient mgr:offline_toggle ; fi
Et voilà! No more messing with offline and online modes in browsers, no more arcane commands to launch, everything just happens automatically.