-1

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 $!";
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
  • It would be useful to have a short, correct, self-contained example. See http://sscce.org/ for the rationale. Write a small program that creates the cache, adds an object, attempts to delete it using one or more or the methods you have tried and that reproduces your error. That would make it much easier to help you. – Bill Ruppert Aug 14 '12 at 02:24
  • I guess I could, but it would just be a minimal www::mechanize::cached program, with the above attempts to delete from the cache in it. – Matthew Lock Aug 14 '12 at 06:02
  • 1
    -1 for lack of working example code that clearly exhibits the problem. It is *your* responsibility to raise the quality of the question to the highest standard possible. - When I first have to cobble something together on my own, and something does not work as expected, I cannot be certain whether the fault is in my extra code or not. Worse, those minutes of extra coding waste my time that I could spend to answer more questions. Even worse, that wasted time is multiplied by each person that attempts to answer the question. – daxim Aug 14 '12 at 09:31

1 Answers1

3

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;
};
daxim
  • 39,270
  • 4
  • 65
  • 132