Mojo-UserAgent-Cached

 view release on metacpan or  search on metacpan

lib/Mojo/UserAgent/Cached.pm  view on Meta::CPAN

        accepted_error_codes
        sorted_queries
    /;

    $ua->created_stacktrace($ua->_get_stacktrace);

    return bless($ua, $class);
}


sub invalidate {
    my ($self, $key) = @_;

    if ($self->is_cacheable($key)) {
        $self->logger->debug("Invalidating cache for '$key'");
        return $self->cache_agent->remove($key);
    }

    return;
}

lib/Mojo/UserAgent/Cached.pm  view on Meta::CPAN


=head2 request_timeout

Defaults to C<$ENV{MOJO_REQUEST_TIMEOUT} // 10>

=head1 METHODS

L<Mojo::UserAgent::Cached> inherits all methods from L<Mojo::UserAgent> and
implements the following new ones.

=head2 invalidate

  $ua->invalidate($key);

Deletes the cache of the given $key.

=head2 expire

  $ua->expire($key);

Set the cache of the given $key as expired.

=head2 set

lib/Mojo/UserAgent/Cached.pm  view on Meta::CPAN

  my $cache_key = $ua->generate_key("http://localhost:$port", ...);
  $ua->set($cache_key, $tx);

Set allows setting data directly for a given URL

=head2 generate_key(@params)

Returns a key to be used for the cache agent. It accepts the same parameters
that a normal ->get() request does.

=head2 validate_key

  my $status = $ua4->validate_key('http://example.com');

Fast validates if key is valid in cache without doing fetch.
Return 1 if true.

=head2 sort_query($url)

Returns a string with the URL passed, with sorted query parameters suitable for cache lookup

=head1 OVERRIDEN METHODS

=head2 new

t/mojo_useragent_cached.t  view on Meta::CPAN

my $ua4 = Mojo::UserAgent::Cached->new();
is $ua4->get('NOT_newsfeed.xml')->res->code, 404, 'Return 404 when file is not found';
is $ua4->get('NOT_newsfeed.xml')->res->body, '',  'Return empty body when file not found';

subtest 'Test against real URL on Mock server' => sub {
    my $ua5 = Mojo::UserAgent::Cached->new();
    $ua5->server->app($app);

    local *Mojo::UserAgent::Cached::is_cacheable = sub { return 1; };

    $ua5->invalidate('/content');

    my $tx1 = $ua5->get('/content');
    my $first_age = $tx1->res->headers->header('X-Mojo-UserAgent-Cached-Age');
    ok !$first_age, 'Not cached first time';

    my $tx2 = $ua5->get('/content');
    my $second_ts = $tx2->res->headers->header('X-Mojo-UserAgent-Cached-Cached');
    my $second_age = $tx2->res->headers->header('X-Mojo-UserAgent-Cached-Age');
    ok $second_age > 0, 'Response is cached';

t/mojo_useragent_cached.t  view on Meta::CPAN


# Set cache directly to avoid redundant call to Vipr
my $ua_with_mock_server = Mojo::UserAgent::Cached->new();
$ua_with_mock_server->server->app($app);
my $tx = $ua_with_mock_server->get("/");

my $cache_key = "http://some_server/this_is_modified_cache_key";
$ua4->set($cache_key, $tx);
is $ua4->get($cache_key)->res->code, 200, 'Can fetch from modified cache key';

# validate cache key
is $ua4->is_valid($cache_key), 1, "Cache key is valid";
$ua4->invalidate($cache_key); # expire
is $ua4->is_valid($cache_key), undef, "Cache key is expired";

is $ua4->is_valid('INVALID_KEY'), undef, 'Invalid key';

# HUH? What is this testing?
is $ua4->is_valid(Mojo::UserAgent->new()), undef, 'Invalid key (object) survives';

subtest 'ABCN-3702' => sub {
    my $ua = Mojo::UserAgent::Cached->new();
    my $tx = $ua->get('t/data/body+headers.txt');

t/mojo_useragent_cached.t  view on Meta::CPAN


subtest 'ABCN-3572' => sub {
    my $ua = Mojo::UserAgent::Cached->new();
    $ua->server->app($app);

    # Allow caching /foo requests too 
    no warnings 'redefine';
    local *Mojo::UserAgent::Cached::is_cacheable = sub { return 1; };

    my $url = "/content/?non-blocking-cache-test";
    $ua->invalidate($url);
    my $tx = $ua->get($url);
    is $tx->res->code, 200, 'right status';
    ok !$tx->res->headers->header('X-Mojo-UserAgent-Cached-Age'), 'First request should not be cached';

    my $first_code = $tx->res->code;
    my $first_body = $tx->res->body;
    my $first_headers = $tx->res->headers;

    my ($headers, $success, $code, $body);
    $ua->get(

t/mojo_useragent_cached.t  view on Meta::CPAN

        is $ua->generate_key(@{$v}), $k, "generate_key " . (join " ", @{$v}) . " => $k";
    }

    # Allow caching /foo requests too
    local *Mojo::UserAgent::Cached::is_cacheable = sub { return 1; };

    my @params = ('/content' => { 'X-Test' => [ 'Test' ] });

    my $cache_key = $ua->generate_key(@params);
    is($cache_key, '/content,{"X-Test":["Test"]}', 'cache key is correct');
    $ua->invalidate($cache_key);

    my $tx1 = $ua->get(@params);
    ok !$tx1->res->headers->header('X-Mojo-UserAgent-Cached-Age'), 'first response is not cached';

    my $tx2 = $ua->get(@params);
    ok $tx2->res->headers->header('X-Mojo-UserAgent-Cached-Age') > 0, 'response is cached';

};

subtest 'Test overridable key generator' => sub {

t/mojo_useragent_cached.t  view on Meta::CPAN

    ok $cached_tx->req->is_finished, 'request is finished';
    ok $cached_tx->res->is_finished, 'response is finished';
};

subtest 'expired+cached functionality' => sub {
    my $ua = Mojo::UserAgent::Cached->new();
    $ua->server->app($app);
    # Allow caching /foo requests too
    local *Mojo::UserAgent::Cached::is_cacheable = sub { return 1; };
    # make sure we have no cache around
    $ua->invalidate($ua->generate_key("/maybe_not_found"));

    ok $ua->is_cacheable("/maybe_not_found"), 'Local URL is cacheable';

    # First normal request
    my $tx = $ua->get("/maybe_not_found");
    is $tx->res->code, '200', 'Get 200 correctly first time';
    my $body = $tx->res->body;
    like $body, qr/\d+\.\d+/, 'Body has timestamp only';

    # ...switch to serving 404

t/mojo_useragent_cached.t  view on Meta::CPAN

};

subtest 'url by url caching' => sub {
   my $ua = Mojo::UserAgent::Cached->new( cache_opts => { expires_in => '1 seconds' }, cache_url_opts => { 'http://.*?/content' => { expires_in => '5 seconds' } } );
   $ua->server->app($app);

   # Allow caching /foo requests too
   no warnings 'redefine';
   local *Mojo::UserAgent::Cached::is_cacheable = sub { return 1; };

   $ua->invalidate($ua->generate_key('/content'));
   my $tx = $ua->get('/content');
   my $first_cached_at = $tx->res->headers->header('X-Mojo-UserAgent-Cached-Cached');

   sleep 1;

   my $tx2 = $ua->get('/content');

   is $tx2->res->headers->header('X-Mojo-UserAgent-Cached-Cached'), $first_cached_at, 'Same cached at time';
   ok $tx2->res->headers->header('X-Mojo-UserAgent-Cached-Age') > 0, 'Has been in cached more than the default 1 seconds';
};



( run in 0.234 second using v1.01-cache-2.11-cpan-0d8aa00de5b )