/usr/share/perl5/App/Cache.pm is in libapp-cache-perl 0.37-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | package App::Cache;
use strict;
use warnings;
use File::Find::Rule;
use File::HomeDir;
use File::Path qw( mkpath );
use File::stat;
use HTTP::Cookies;
use LWP::UserAgent;
use Path::Class;
use Storable qw(nstore retrieve);
use base qw( Class::Accessor::Chained::Fast );
__PACKAGE__->mk_accessors(qw( application directory ttl enabled ));
our $VERSION = '0.37';
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
unless ( $self->application ) {
my $caller = (caller)[0];
$self->application($caller);
}
unless ( $self->directory ) {
my $dir = dir( home(), "." . $self->_clean( $self->application ),
"cache" );
$self->directory($dir);
}
my $dir = $self->directory;
unless ( -d "$dir" ) {
mkpath("$dir")
|| die "Error mkdiring " . $self->directory . ": $!";
}
unless ( defined $self->enabled ) {
$self->enabled(1);
}
return $self;
}
sub clear {
my $self = shift;
foreach
my $filename ( File::Find::Rule->new->file->in( $self->directory ) )
{
unlink($filename) || die "Error unlinking $filename: $!";
}
foreach my $dirname ( sort { length($b) <=> length($a) }
File::Find::Rule->new->directory->in( $self->directory ) )
{
next if $dirname eq $self->directory;
rmdir($dirname) || die "Error unlinking $dirname: $!";
}
}
sub delete {
my ( $self, $key ) = @_;
my $filename = $self->_clean_filename($key);
return unless -f $filename;
unlink($filename) || die "Error unlinking $filename: $!";
}
sub get {
my ( $self, $key ) = @_;
return unless $self->enabled;
my $ttl = $self->ttl || 60 * 30; # default ttl of 30 minutes
my $filename = $self->_clean_filename($key);
return undef unless -f $filename;
my $now = time;
my $stat = stat($filename) || die "Error stating $filename: $!";
my $ctime = $stat->ctime;
my $age = $now - $ctime;
if ( $age < $ttl ) {
my $value = retrieve("$filename")
|| die "Error reading from $filename: $!";
return $value->{value};
} else {
$self->delete($key);
return undef;
}
}
sub get_code {
my ( $self, $key, $code ) = @_;
my $data = $self->get($key);
unless ($data) {
$data = $code->();
$self->set( $key, $data );
}
return $data;
}
sub get_url {
my ( $self, $url ) = @_;
my $data = $self->get($url);
unless ($data) {
my $ua = LWP::UserAgent->new;
$ua->cookie_jar( HTTP::Cookies->new() );
my $response = $ua->get($url);
if ( $response->is_success ) {
$data = $response->content;
} else {
die "Error fetching $url: " . $response->status_line;
}
$self->set( $url, $data );
}
return $data;
}
sub scratch {
my $self = shift;
my $directory = $self->_clean_filename("_scratch");
unless ( -d $directory ) {
mkdir($directory) || die "Error mkdiring $directory: $!";
}
return $directory;
}
sub set {
my ( $self, $key, $value ) = @_;
return unless $self->enabled;
my $filename = $self->_clean_filename($key);
nstore( { value => $value }, "$filename" )
|| die "Error writing to $filename: $!";
}
sub _clean {
my ( $self, $text ) = @_;
$text = lc $text;
$text =~ s/[^a-z0-9]+/_/g;
return $text;
}
sub _clean_filename {
my ( $self, $key ) = @_;
$key = $self->_clean($key);
my $filename = file( $self->directory, $key );
return $filename;
}
1;
__END__
=head1 NAME
App::Cache - Easy application-level caching
=head1 SYNOPSIS
# in your class:
my $cache = App::Cache->new({ ttl => 60*60 });
$cache->delete('test');
my $data = $cache->get('test');
my $code = $cache->get_code("code", sub { $self->calculate() });
my $html = $cache->get_url("http://www.google.com/");
$cache->set('test', 'one');
$cache->set('test', { foo => 'bar' });
my $scratch = $cache->scratch;
$cache->clear;
=head1 DESCRIPTION
The L<App::Cache> module lets an application cache data locally. There
are a few times an application would need to cache data: when it is
retrieving information from the network or when it has to complete a
large calculation.
For example, the L<Parse::BACKPAN::Packages> module downloads a file off
the net and parses it, creating a data structure. Only then can it
actually provide any useful information for the programmer.
L<Parse::BACKPAN::Packages> uses L<App::Cache> to cache both the file
download and data structures, providing much faster use when the data is
cached.
This module stores data in the home directory of the user, in a dot
directory. For example, the L<Parse::BACKPAN::Packages> cache is
actually stored underneath "~/.parse_backpan_packages/cache/". This is
so that permisssions are not a problem - it is a per-user,
per-application cache.
=head1 METHODS
=head2 new
The constructor creates an L<App::Cache> object. It takes three optional
parameters:
=over
=item *
ttl contains the number of seconds in which a cache entry expires. The default
is 30 minutes.
my $cache = App::Cache->new({ ttl => 30*60 });
=item *
application sets the application name. If you are calling new() from a class,
the application is automagically set to the calling class, so you should rarely
need to pass it in:
my $cache = App::Cache->new({ application => 'Your::Module' });
=item *
directory sets the directory to be used for the cache. Normally this is just
set for you and will be based on the application name and be created in the
users home directory. Sometimes for testing, it can be useful to set this.
my $cache = App::Cache->new({ directory => '/tmp/your/cache/dir' });
=item *
enabled can be set to 0 for testing, in which case you will always get
cache misses:
my $cache = App::Cache->new({ enabled => 0 });
=back
=head2 clear
Clears the cache:
$cache->clear;
=head2 delete
Deletes an entry in the cache:
$cache->delete('test');
=head2 get
Gets an entry from the cache. Returns undef if the entry does not exist
or if it has expired:
my $data = $cache->get('test');
=head2 get_code
This is a convenience method. Gets an entry from the cache, but if the
entry does not exist, set the entry to the value of the code reference
passed:
my $code = $cache->get_code("code", sub { $self->calculate() });
=head2 get_url
This is a convenience method. Gets the content of a URL from the cache,
but if the entry does not exist, set the entry to the content of the URL
passed:
my $html = $cache->get_url("http://www.google.com/");
=head2 scratch
Returns a directory in the cache that the application may use for
scratch files:
my $scratch = $cache->scratch;
=head2 set
Set an entry in the cache. Note that an entry value may be an arbitrary
Perl data structure:
$cache->set('test', 'one');
$cache->set('test', { foo => 'bar' });
=head2 directory
Returns the full path to the cache directory. Primarily useful for when you
are writing tests that use App::Cache and want to clean up after yourself. If
you are doing that you may want to explicitly set the 'application' constructor
parameter to avoid later cleaning up a cache dir that was already in use.
my $dir = $cache->directory;
=head1 AUTHOR
Leon Brocard <acme@astray.com>
=head1 COPYRIGHT
Copyright (C) 2005-7, Leon Brocard
=head1 LICENSE
This module is free software; you can redistribute it or modify it under
the same terms as Perl itself.
|