Last active
August 29, 2015 14:06
-
-
Save olegwtf/3eb8b6625126dddde15c to your computer and use it in GitHub Desktop.
LWP::UserAgent::Cached and If-Modified-Since header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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