Cache/Action.pm

filter: Statementstmt 57.6% 34 / 59 Branchbran 25.0% 5 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 50.0% 47 / 94 CC 12 SCAR 29.7
CC
12
Cov
48%
__ANON__
42.0
register
19.9
__ANON__
6.0
CRAP 19.5
« Cache/Page.pm ↑ Summary Cache.pm »
1 package Mojolicious::Plugin::Cache::Action;
2 {
3 1 $Mojolicious::Plugin::Cache::Action::VERSION = '0.0017'; # TRIAL
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use base qw/Mojolicious::Plugin/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Module implementation
14 #
15  
16 1 my ( $actions, $cache );
17 1 has 'driver' => 'Memory';
18  
19 1 ▶sub register {
Subroutine register: called CC=6 SCAR=29.9
Pod documented
20 1 my ( $self, $app, $conf ) = @_;
21  
22 1 ▶ if ( defined $conf->{actions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"actions"}) 0 1 1
23 0 $actions = { map { $_ => 1 } @{ $conf->{actions} } };
24 }
25  
26 #setup cache
27 1 ▶ if ( !$cache ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache) 1 0 1
28 1 ▶ if ( defined $conf->{options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{'options'}) { } 1 0 1
29 1 my $opt = $conf->{options};
30 1 ▶ $opt->{driver} = $self->driver if not defined $opt->{driver};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"driver"} 0 1 1
31 1 $cache = CHI->new(%$opt);
32 }
33 else {
34 0 $cache = CHI->new( driver => $self->driver );
35 }
36 }
37  
38 1 ▶ if ( $app->log->level eq 'debug' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->log->level eq "debug") 0 1 1
39 0 $cache->on_set_error('log');
40 0 $cache->on_get_error('log');
41 }
42  
43 $app->hook(
44 0 ▶ 'before_dispatch' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
45 0 my ($c) = @_;
46 0 my $path = $c->req->url->to_abs->to_string;
47 0 $app->log->debug( ref $path );
48 0 ▶ if ( $cache->is_valid($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->is_valid($path)) 0 0 0
49 0 $app->log->debug("serving from cache for $path");
50 0 my $data = $cache->get($path);
51 0 $c->res->code( $data->{code} );
52 0 $c->res->headers( $data->{headers} );
53 0 $c->res->body( $data->{body} );
54 0 $c->stash( 'from_cache' => 1 );
55 }
56 }
57 1 );
58  
59 $app->hook(
60 0 ▶ 'after_dispatch' => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
61 0 my ($c) = @_;
62  
63 #conditions at which no caching will be done
64 ## - it is already a cached response
65 0 ▶ return if $c->stash('from_cache');
Branch: 1 branch
BranchTrueFalseTotal
if $c->stash("from_cache") 0 0 0
66  
67 ## - has to be GET request
68 0 ▶ return if $c->req->method ne 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->method ne "GET" 0 0 0
69  
70 ## - only successful response
71 0 ▶ return if $c->res->code != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $c->res->code != 200 0 0 0
72  
73 0 my $path = $c->req->url->to_abs->to_string;
74 0 my $name = $c->stash('action');
75  
76 ## - have to match the action
77 return
78 if defined $conf->{actions}
79 0 ▶ and not exists $actions->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"actions"} and not exists $actions->{$name} 0 0 0
Condition0%0/3
defined $conf->{"actions"} and not exists $actions->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conf->{"actions"} and not exists $actions->{$name}
defined $conf->{"actions"} not exists $actions->{$name}
Truth table0%0/3
A and not(B)
A= defined $conf->{"actions"}B= not exists $actions->{$name}
ABresult
0X0
100
111
80  
81 0 $app->log->debug("storing in cache for $path and action $name");
82 0 $cache->set(
83 $path,
84 { body => $c->res->body,
85 headers => $c->res->headers,
86 code => $c->res->code
87 }
88 );
89 }
90 1 );
91  
92 1 return;
93 }
94  
95 1 1;
96  
97 # ABSTRACT: Action caching plugin
98  
99 __END__
« Cache/Page.pm ↑ Summary Cache.pm »