I am using WWW::Mechanize::Cached together with Cache::FileCache, and I'd like to remove certain URLs from the cache sometimes but WWW::Mechanize::Cached doesn't have such an option.
I've looked through the source code, and can see that the caching is set using this line:
$self->cache->set( $req, freeze( $response ) ) if $should_cache;
So I've tried using the following code to remove an item from the cache:
$cache->remove($mech->response->request) or warn "cannot remove $!";
or
$cache->remove($mech->response->request->as_string) or warn "cannot remove $!";
But I get the warning: "cannot remove No such file or directory".
I've also found the following ideas, but none seem to work https://groups.google.com/forum/?fromgroups#!topic/perl-cache-discuss/M_wXFNL5MdM[1-25]
if ( $want_to_delete_url ) {
$mech->cache->remove( $url );
}
$mech->get( $url );
http://www.perlmonks.org/?node_id=564208
my $url = "http://www.rulez.sk/headers.php";
my $req = GET $url, 'Accept-Encoding' => 'identity';
$cache->remove($req->as_string) or print "cannot remove $!";
It helps to read the documentation of the software you're working with. CHI lists all cache keys with the get_keys
method, so you can simply iterate over them until you find the one you want.
use 5.010;
use CHI qw();
use HTTP::Request qw();
use WWW::Mechanize::Cached qw();
my $cache = CHI->new(
driver => 'CacheCache',
cc_class => 'Cache::FileCache',
cc_options => { cache_root => '/tmp' },
);
my $uri = 'http://www.iana.org/domains/example/';
my $mech = WWW::Mechanize::Cached->new(cache => $cache);
$mech->get($uri);
for my $key ($cache->get_keys) {
my $r = HTTP::Request->parse($key);
say $r->uri;
$cache->remove($key) if $r->uri eq $uri;
};