Skip to content

Instantly share code, notes, and snippets.

@olegwtf
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegwtf/3eb8b6625126dddde15c to your computer and use it in GitHub Desktop.
Save olegwtf/3eb8b6625126dddde15c to your computer and use it in GitHub Desktop.
LWP::UserAgent::Cached and If-Modified-Since header
use strict;
use LWP::UserAgent::Cached 0.06;
use HTTP::Date;
mkdir '/tmp/cache';
my $ua = LWP::UserAgent::Cached->new(cache_dir => '/tmp/cache');
$ua->nocache_if(sub {
my $resp = shift;
$resp->code == 304; # do not cache not modified
});
$ua->recache_if(sub {
my ($resp, $cached_file_path, $req) = @_;
if ($req->header('Cache-Control') eq 'no-cache') {
# bypass caching
return 1;
}
# try to recache always by sending If-Modified-Since header
$req->header('If-Modified-Since', time2str((stat($cached_file_path))[9]));
1;
});
$ua->get("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"); # test it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment