MooX::Role::CachedURL - a role providing a locally cached copy of a remote file
package MyClass; use Moo; with 'MooX::Role::CachedURL'; has '+url' => (default => sub { 'http://www.cpan.org/robots.txt' }); sub my_method { my $self = shift; my $fh = $self->open_file; while (<$fh>) { ... } $self->close_file($fh); }
Then in the user of MyClass:
use MyClass; my $object = MyClass->new(max_age => '2 days'); print "local file is ", $object->cache_path, "\n";
This role represents a remote file that you want to cache locally, and then process. This is common functionality that I'm pulling out of my PAUSE::Users, PAUSE::Permissions and PAUSE::Packages modules.
PAUSE::Users provides a simple interface to the 00whois.xml file that is generated by PAUSE. It caches the file locally, then provides a mechanism for iterating over all users in the file.
00whois.xml
The full path to the local file where the content of the remote URL will be cached. You can provide your own path, but if you don't, then an appropriate path for your operating system will be generated.
A full or relative path to your own copy of the cached content. If you provide this, then your content will be used, without checking the remote URL. If the file you pass doesn't exist, the module will croak().
croak()
This specifies the URL that should be cached locally. It should be over-ridden in the composing class, as shown in the SYNOPSIS above.
Specifies the maximum age of the local copy, in seconds. We won't even look for a new remote copy if the cached copy is younger than this.
You can specify max_age using any of the notations supported by Time::Duration::Parse. For example:
max_age => '2 hours',
If the cache_path or path attribute ends in .gz, then the file is assumed to be gzip'd, and will be transparently handled using PerlIO::gzip.
cache_path
path
.gz
Switch to LWP for general URL handling, not just HTTP
Ability for a class to transform content when caching
https://github.com/neilbowers/MooX-Role-CachedURL
Neil Bowers <neilb@cpan.org>
This software is copyright (c) 2014 by Neil Bowers <neilb@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install MooX::Role::CachedURL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooX::Role::CachedURL
CPAN shell
perl -MCPAN -e shell install MooX::Role::CachedURL
For more information on module installation, please visit the detailed CPAN module installation guide.