| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CHI::Route; |
|
2
|
2
|
|
|
2
|
|
1919
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
22
|
|
|
3
|
2
|
|
|
2
|
|
500
|
use Mojo::Util qw'md5_sum'; |
|
|
2
|
|
|
|
|
17
|
|
|
|
2
|
|
|
|
|
118
|
|
|
4
|
2
|
|
|
2
|
|
13
|
use Mojo::Date; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
21
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Register plugin |
|
9
|
|
|
|
|
|
|
sub register { |
|
10
|
2
|
|
|
2
|
1
|
272590
|
my ($plugin, $app, $param) = @_; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Plugin parameter |
|
13
|
2
|
|
50
|
|
|
13
|
$param ||= {}; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Load parameter from Config file |
|
16
|
2
|
50
|
|
|
|
12
|
if (my $config_param = $app->config('CHI-Route')) { |
|
17
|
0
|
|
|
|
|
0
|
$param = { %$param, %$config_param }; |
|
18
|
|
|
|
|
|
|
}; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Load CHI plugin if not already loaded |
|
21
|
2
|
50
|
|
|
|
36
|
unless (exists $app->renderer->helpers->{chi}) { |
|
22
|
0
|
|
|
|
|
0
|
$app->plugin('CHI'); |
|
23
|
|
|
|
|
|
|
}; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# set namespace |
|
26
|
2
|
|
50
|
|
|
25
|
my $namespace = $param->{namespace} // 'default'; |
|
27
|
|
|
|
|
|
|
|
|
28
|
2
|
100
|
|
|
|
16
|
unless ($app->chi($namespace)) { |
|
29
|
1
|
|
|
|
|
205
|
$app->log->error("No cache defined for '$namespace'"); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Add condition to check for cache |
|
32
|
|
|
|
|
|
|
$app->routes->add_condition( |
|
33
|
2
|
|
|
2
|
|
35101
|
chi => sub { 1 } |
|
34
|
1
|
|
|
|
|
19
|
); |
|
35
|
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
40
|
return; |
|
37
|
|
|
|
|
|
|
}; |
|
38
|
|
|
|
|
|
|
|
|
39
|
1
|
|
50
|
|
|
192
|
my $expires_in = $param->{expires_in} // '6 hours'; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Set default key |
|
42
|
|
|
|
|
|
|
my $default_key = $param->{key} // sub { |
|
43
|
2
|
|
|
2
|
|
6
|
return shift->req->url->to_abs->to_string; |
|
44
|
1
|
|
50
|
|
|
26
|
}; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Cache if required |
|
47
|
|
|
|
|
|
|
$app->hook( |
|
48
|
|
|
|
|
|
|
after_render => sub { |
|
49
|
12
|
|
|
12
|
|
2745
|
my ($c, $output, $format) = @_; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# No cache instruction found |
|
52
|
12
|
100
|
|
|
|
35
|
return unless $c->stash('chi.r.cache'); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Only cache successfull renderings |
|
55
|
5
|
100
|
66
|
|
|
88
|
if ($c->res->is_error || ($c->stash('status') && $c->stash('status') != 200)) { |
|
|
|
|
66
|
|
|
|
|
|
56
|
2
|
|
|
|
|
81
|
return; |
|
57
|
|
|
|
|
|
|
}; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Get key from stash |
|
60
|
3
|
|
|
|
|
126
|
my $key = $c->stash('chi.r.cache'); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Set ETag and last_modified headers |
|
63
|
3
|
|
|
|
|
37
|
my $last_modified = Mojo::Date->new; |
|
64
|
3
|
|
|
|
|
31
|
my $headers; |
|
65
|
3
|
|
|
|
|
15
|
foreach ($c->res->headers) { |
|
66
|
3
|
|
|
|
|
65
|
$_->last_modified($last_modified); |
|
67
|
3
|
|
|
|
|
43
|
$_->etag('W/"' . md5_sum($last_modified->to_string). '"'); |
|
68
|
3
|
|
|
|
|
169
|
$headers = $_->to_hash; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Delete Mojolicious server header |
|
71
|
3
|
|
|
|
|
242
|
delete $headers->{Server}; |
|
72
|
|
|
|
|
|
|
}; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Cache |
|
75
|
3
|
|
66
|
|
|
13
|
$c->chi($namespace)->set( |
|
76
|
|
|
|
|
|
|
$key => { |
|
77
|
|
|
|
|
|
|
'body' => $$output, |
|
78
|
|
|
|
|
|
|
'format' => $format, |
|
79
|
|
|
|
|
|
|
'headers' => $headers |
|
80
|
|
|
|
|
|
|
} => { |
|
81
|
|
|
|
|
|
|
expires_in => $c->stash('chi.r.expires') // $expires_in |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
); |
|
84
|
|
|
|
|
|
|
} |
|
85
|
1
|
|
|
|
|
24
|
); |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Add condition to check for cache |
|
88
|
|
|
|
|
|
|
$app->routes->add_condition( |
|
89
|
|
|
|
|
|
|
chi => sub { |
|
90
|
14
|
|
|
14
|
|
149587
|
my ($r, $c, $captures, $arg) = @_; |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Only cache GET requests |
|
93
|
14
|
50
|
|
|
|
53
|
return 1 if $c->req->method ne 'GET'; |
|
94
|
|
|
|
|
|
|
|
|
95
|
14
|
|
|
|
|
223
|
my $chi = $c->chi($namespace); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# No cache associated |
|
98
|
14
|
50
|
|
|
|
279
|
return 1 unless $chi; |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Get the key for the cache |
|
101
|
14
|
|
|
|
|
26
|
my $key; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Set by argument |
|
104
|
14
|
100
|
|
|
|
38
|
if ($arg->{key}) { |
|
105
|
12
|
100
|
|
|
|
49
|
$key = ref $arg->{key} ? $arg->{key}->($c) : $arg->{key} . ''; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Set |
|
109
|
|
|
|
|
|
|
else { |
|
110
|
2
|
|
|
|
|
7
|
$key = $default_key->($c); |
|
111
|
|
|
|
|
|
|
}; |
|
112
|
|
|
|
|
|
|
|
|
113
|
14
|
100
|
|
|
|
1374
|
return 1 unless $key; |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Get cache based on key |
|
116
|
12
|
|
|
|
|
62
|
my $found = $chi->get($key); |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Found cache! Render |
|
119
|
12
|
100
|
|
|
|
1335
|
if ($found) { |
|
120
|
|
|
|
|
|
|
|
|
121
|
7
|
|
|
|
|
32
|
$c->stash->{'mojo.routed'} = 1; |
|
122
|
|
|
|
|
|
|
|
|
123
|
7
|
|
|
|
|
73
|
my $headers = $found->{headers}; |
|
124
|
7
|
|
|
|
|
17
|
my $etag = delete $headers->{'ETag'}; |
|
125
|
7
|
|
|
|
|
11
|
my $last_modified = delete $headers->{'Last-Modified'}; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Check if client side cache is still fresh |
|
128
|
7
|
100
|
|
|
|
28
|
if ($c->is_fresh( |
|
129
|
|
|
|
|
|
|
etag => $etag, |
|
130
|
|
|
|
|
|
|
last_modified => $last_modified |
|
131
|
|
|
|
|
|
|
)) { |
|
132
|
2
|
|
|
|
|
494
|
$c->log->debug('Client side cache is still valid'); |
|
133
|
2
|
|
|
|
|
63
|
$c->rendered(304); |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Client has no valid copy of the cache |
|
137
|
|
|
|
|
|
|
else { |
|
138
|
|
|
|
|
|
|
|
|
139
|
5
|
|
|
|
|
945
|
$c->log->debug('Routing to a cache'); |
|
140
|
|
|
|
|
|
|
|
|
141
|
5
|
|
|
|
|
179
|
for ($c->res) { |
|
142
|
5
|
|
|
|
|
55
|
$_->headers->from_hash($headers); |
|
143
|
5
|
|
|
|
|
178
|
$_->headers->header('X-Cache-CHI' => 1); |
|
144
|
5
|
|
|
|
|
150
|
$_->code(200); |
|
145
|
|
|
|
|
|
|
}; |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Render from cache |
|
148
|
|
|
|
|
|
|
$c->render( |
|
149
|
|
|
|
|
|
|
'format' => $found->{format}, |
|
150
|
|
|
|
|
|
|
'data' => $found->{body} |
|
151
|
5
|
|
|
|
|
47
|
); |
|
152
|
|
|
|
|
|
|
}; |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Skip to final |
|
155
|
7
|
|
|
|
|
1563
|
$c->match->position(1000); |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Render normally and then cache the route |
|
159
|
|
|
|
|
|
|
else { |
|
160
|
|
|
|
|
|
|
|
|
161
|
5
|
100
|
|
|
|
16
|
if (exists $arg->{expires_in}) { |
|
162
|
1
|
|
|
|
|
5
|
$c->stash('chi.r.expires' => $arg->{expires_in}); |
|
163
|
|
|
|
|
|
|
}; |
|
164
|
|
|
|
|
|
|
|
|
165
|
5
|
|
|
|
|
36
|
$c->stash('chi.r.cache' => $key); |
|
166
|
|
|
|
|
|
|
}; |
|
167
|
|
|
|
|
|
|
|
|
168
|
12
|
|
|
|
|
176
|
return 1; |
|
169
|
|
|
|
|
|
|
} |
|
170
|
1
|
|
|
|
|
30
|
); |
|
171
|
|
|
|
|
|
|
}; |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
__END__ |