| 1 | package Dancer::Plugin::Cache::CHI; | ||||||||||||||||||||||||||||||
| 2 | BEGIN { | ||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | $Dancer::Plugin::Cache::CHI::AUTHORITY = 'cpan:YANICK'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||||
| 6 | 6 | $Dancer::Plugin::Cache::CHI::VERSION = '1.4.0'; | |||||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Dancer plugin to cache response content (and anything else) | ||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | no warnings qw/ uninitialized /; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Dancer 1.1904 ':syntax'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 6 | my $dancer_version = int Dancer->VERSION; | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | use CHI; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | if ( $dancer_version < 2 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 26 | 6 | require Dancer::Hook; | |||||||||||||||||||||||||||||
| 27 | 6 | require Dancer::Factory::Hook; | |||||||||||||||||||||||||||||
| 28 | 6 | require Dancer::Response; | |||||||||||||||||||||||||||||
| 29 | 6 | require Dancer::SharedData; | |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||||
| 32 | 0 | with 'Dancer::Plugin'; | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||
| 34 | 0 | register_hook 'before_create_cache'; | |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||
| 38 | 6 | my %cache; | |||||||||||||||||||||||||||||
| 39 | my $cache_page; # actually hold the ref to the args | ||||||||||||||||||||||||||||||
| 40 | my $cache_page_key_generator = sub { | ||||||||||||||||||||||||||||||
| 41 | 40 | ▶ | return request()->{path_info}; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 42 | 6 | }; | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | hook after => sub { | ||||||||||||||||||||||||||||||
| 45 | 31 | ▶ | return unless $cache_page; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 13 | my $resp = shift; | |||||||||||||||||||||||||||||
| 48 | 13 | cache()->set( $cache_page_key_generator->(), | |||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||||
| 50 | status => $resp->status, | ||||||||||||||||||||||||||||||
| 51 | headers => $resp->headers_to_array, | ||||||||||||||||||||||||||||||
| 52 | content => $resp->content | ||||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||||
| 54 | @$cache_page, | ||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | 13 | $cache_page = undef; | |||||||||||||||||||||||||||||
| 58 | 6 | }; | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | register cache => sub { | ||||||||||||||||||||||||||||||
| 61 | 59 | ▶ | shift if $dancer_version >= 2; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 62 | 59 | ▶ | return $cache{$_[0]//''} ||= _create_cache( @_ ); | ||||||||||||||||||||||||||||
|
Condition60%3/5
$cache{$_[0] // ""} ||= _create_cache(@_)
$_[0] // ""
MC/DC0%0/2
$cache{$_[0] // ""} ||= _create_cache(@_)
$cache{$_[0] // ""}
_create_cache(@_)
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table66%2/3
A ||= B
A= $cache{$_[0] // ""}B= _create_cache(@_)
Truth table50%1/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||
| 63 | 6 | }; | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 6 | my $honor_no_cache = 0; | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | sub _create_cache { | ||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | my $namespace = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_create_cache: called CC=6 SCAR=19.1
| |||||||||||||||||||||||||||||||
| 69 | 4 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if ( $dancer_version < 2 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 72 | 4 | Dancer::Factory::Hook->execute_hooks( 'before_create_cache' ); | |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||||
| 75 | 0 | execute_hook 'before_create_cache'; | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | 4 | my %setting = %{ plugin_setting() }; | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | $setting{namespace} = $namespace if defined $namespace; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | while( my ( $k, $v ) = each %$args ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 83 | 0 | $setting{$k} = $v; | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | $honor_no_cache = delete $setting{honor_no_cache} | ||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if exists $setting{honor_no_cache}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | 4 | return CHI->new(%setting); | |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | register check_page_cache => sub { | ||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | shift if $dancer_version >= 2; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | my $hook = sub { | ||||||||||||||||||||||||||||||
| 98 | 32 | ▶ | my $dsl = shift if $dancer_version >= 2; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=23.4
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | # Instead halt() now we use a more correct method - setting of a | ||||||||||||||||||||||||||||||
| 101 | # response to Dancer::Response object for a more correct returning of | ||||||||||||||||||||||||||||||
| 102 | # some HTTP headers (X-Powered-By, Server) | ||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | 32 | ▶ | my $cached = cache()->get( $cache_page_key_generator->() ) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 105 | or return; | ||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | 9 | ▶ | if ( $honor_no_cache ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | 5 | $DB::single = 1; | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | 5 | ▶ | my $req = $dancer_version >=2 | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 111 | ? $dsl->request | ||||||||||||||||||||||||||||||
| 112 | : Dancer::SharedData->request | ||||||||||||||||||||||||||||||
| 113 | ; | ||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||
| 115 | return if grep { | ||||||||||||||||||||||||||||||
| 116 | # eval is there to protect from a regression in Dancer 1.31 | ||||||||||||||||||||||||||||||
| 117 | # where headers can be undef | ||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | eval { $req->header($_) eq 'no-cache' } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 119 | } qw/ Cache-Control Pragma /; | ||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | 7 | ▶ | if ( $dancer_version < 2 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 123 | Dancer::SharedData->response( | ||||||||||||||||||||||||||||||
| 124 | Dancer::Response->new( | ||||||||||||||||||||||||||||||
| 125 | ref $cached eq 'HASH' | ||||||||||||||||||||||||||||||
| 126 | ? | ||||||||||||||||||||||||||||||
| 127 | ( | ||||||||||||||||||||||||||||||
| 128 | status => $cached->{status}, | ||||||||||||||||||||||||||||||
| 129 | headers => $cached->{headers}, | ||||||||||||||||||||||||||||||
| 130 | content => $cached->{content} | ||||||||||||||||||||||||||||||
| 131 | ) | ||||||||||||||||||||||||||||||
| 132 | 7 | ▶ | : | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 133 | ( content => $cached ) | ||||||||||||||||||||||||||||||
| 134 | ) | ||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||||
| 137 | $dsl->response( | ||||||||||||||||||||||||||||||
| 138 | Dancer::Core::Response->new( | ||||||||||||||||||||||||||||||
| 139 | is_halted => 1, | ||||||||||||||||||||||||||||||
| 140 | ref $cached eq 'HASH' | ||||||||||||||||||||||||||||||
| 141 | ? | ||||||||||||||||||||||||||||||
| 142 | ( | ||||||||||||||||||||||||||||||
| 143 | status => $cached->{status}, | ||||||||||||||||||||||||||||||
| 144 | headers => $cached->{headers}, | ||||||||||||||||||||||||||||||
| 145 | content => $cached->{content} | ||||||||||||||||||||||||||||||
| 146 | ) | ||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | : | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 148 | ( content => $cached ) | ||||||||||||||||||||||||||||||
| 149 | ) | ||||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||
| 152 | 3 | }; | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | 3 | hook before => $hook; | |||||||||||||||||||||||||||||
| 155 | 6 | }; | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||
| 158 | register cache_page => sub { | ||||||||||||||||||||||||||||||
| 159 | 13 | ▶ | shift if $dancer_version >= 2; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | 13 | my ( $content, @args ) = @_; | |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||
| 163 | 13 | $cache_page = \@args; | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||
| 165 | 13 | return $content; | |||||||||||||||||||||||||||||
| 166 | 6 | }; | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | register cache_page_key => sub { | ||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | shift if $dancer_version >= 2; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 172 | 0 | return $cache_page_key_generator->(); | |||||||||||||||||||||||||||||
| 173 | 6 | }; | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | register cache_page_key_generator => sub { | ||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | shift if $dancer_version >= 2; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 178 | 1 | $cache_page_key_generator = shift; | |||||||||||||||||||||||||||||
| 179 | 6 | }; | |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||
| 182 | 6 | for my $method ( qw/ set get remove clear compute / ) { | |||||||||||||||||||||||||||||
| 183 | register 'cache_'.$method => sub { | ||||||||||||||||||||||||||||||
| 184 | 11 | ▶ | shift if $dancer_version >= 2; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 185 | 11 | return cache()->$method( @_ ); | |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||
| 187 | 30 | } | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | 6 | ▶ | Dancer::Factory::Hook->instance->install_hooks(qw/ before_create_cache /) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 190 | if $dancer_version < 2; | ||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||
| 193 | 6 | register_plugin for_versions => [1,2]; | |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||||