For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > September 2004 > HTTP::Cache module









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author HTTP::Cache module
Mattias Holmlund

2004-09-28, 4:00 pm

Hi,

I have written a perl module that I want to publish on CPAN. The module
implements a cache for http requests. It provides a single method get
that fetches a url via http. The result of each get is stored in a
cache on disk, and if the same url has been requested before, the
proper ETag and If-Modified-Since headers are sent to the http server.
The server can then respond that the object in the cache is up-to-date
and HTTP::Cache will return a cached version of the data instead of
fetching it from the server. This speeds up the HTTP get and saves
bandwidth for both the server and the client.

The module is very simple to use. Simply create a HTTP::Cache object
and call the get-method on the returned object to fetch a url. You do
not have to care if the data is fetched from the server or from the
cache (but you can find out if you want to know).

Sample usage:

my $c = HTTP::Cache->new( {
BasePath => "/tmp/cache", # Directory to store the cache in.
MaxAge => 8*24, # How many hours should items be
# kept in the cache after they
# were last accessed?
# Default is 8*24.
Verbose => 1, # Print messages to STDERR.
# Default is 0.
UserAgent => "my-spider", # The user-agent string to use.
# Default is "perl-http-cache".
} );

my( $content, $error ) = $c->get( $url );

if( defined( $content ) )
{
# Data retrieved and stored in $content.
# $error indicates if the data was found in the cache (0)
# if it was fetched from the server but equal to the cache (1)
# or if it was fetched from the server and different from the
# cache (2).
}
else
{
print STDERR "Failed to fetch $url. " .
"Error returned by server: $error";
}

Does anyone object to putting this module on CPAN or is it redundant?
Is HTTP::Cache a good name for it?

Regards,

Mattias Holmlund

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com