HTTP::Cache::Transparent(3pm) | User Contributed Perl Documentation | HTTP::Cache::Transparent(3pm) |
HTTP::Cache::Transparent - Cache the result of http get-requests persistently.
use LWP::Simple; use HTTP::Cache::Transparent; HTTP::Cache::Transparent::init( { BasePath => '/tmp/cache', } ); my $data = get( 'http://www.sn.no' );
An implementation of http get that keeps a local cache of fetched pages to avoid fetching the same data from the server if it hasn't been updated. The cache is stored on disk and is thus persistent between invocations.
Uses the http-headers If-Modified-Since and ETag to let the server decide if the version in the cache is up-to-date or not.
The cache is implemented by modifying the LWP::UserAgent class to seamlessly cache the result of all requests that can be cached.
HTTP::Cache::Transparent provides an init-method that sets the parameters for the cache and overloads a method in LWP::UserAgent to activate the cache.After init has been called, the normal LWP-methods (LWP::Simple as well as the more full-fledged LWP::Request methods) should be used as usual.
HTTP::Cache::Transparent::init( { # Directory to store the cache in. BasePath => "/tmp/cache", # How many hours should items be kept in the cache # after they were last requested? # Default is 8*24. MaxAge => 8*24, # Print progress-messages to STDERR. # Default is 0. Verbose => 1, # If a request is made for a url that has been requested # from the server less than NoUpdate seconds ago, the # response will be generated from the cache without # contacting the server. # Default is 0. NoUpdate => 15*60, # When a url has been downloaded and the response indicates that # has been modified compared to the content in the cache, # the ApproveContent callback is called with the HTTP::Response. # The callback shall return true if the response shall be used and # stored in the cache or false if the response shall be discarded # and the response in the cache used instead. # This mechanism can be used to work around servers that return errors # intermittently. The default is to accept all responses. ApproveContent => sub { return $_[0]->is_success }, } );
The directory where the cache is stored must be writable. It must also only contain files created by HTTP::Cache::Transparent.
use HTTP::Cache::Transparent ( BasePath => '/tmp/cache' );
which is exactly equivalent to
use HTTP::Cache::Transparent; HTTP::Cache::Transparent::init( BasePath => '/tmp/cache' );
The advantage to using this method is that you can do
perl -MHTTP::Cache::Transparent=BasePath,/tmp/cache myscript.pl
or even set the environment variable PERL5OPT
PERL5OPT=-MHTTP::Cache::Transparent=BasePath,/tmp/cache myscript.pl
and have all the http-requests performed by myscript.pl go through the cache without changing myscript.pl
The HTTP::Cache::Transparent inserts three special headers in the HTTP::Response object. These can be accessed via the HTTP::Response::header()-method.
This module has a number of limitations that you should be aware of before using it.
The cache is stored on disk as one file per cached object. The filename is equal to the md5sum of the url and the Range-header if it exists. The file contains a set of key/value-pairs with metadata (one entry per line) followed by a blank line and then the actual data returned by the server.
The last modified date of the cache file is set to the time when the cache object was last requested by a user.
Mattias Holmlund, <$firstname -at- $lastname -dot- se> <http://www.holmlund.se/mattias/>
A git repository containing the source for this module can be found via http://git.holmlund.se/
Copyright (C) 2004-2007 by Mattias Holmlund
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.
2022-12-06 | perl v5.36.0 |