line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Cache::Page; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Mojolicious::Plugin::Cache::Page::VERSION = '0.0017'; # TRIAL |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
445754
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
7
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
77
|
|
8
|
1
|
|
|
1
|
|
7
|
use File::Path qw/make_path/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
52
|
|
9
|
1
|
|
|
1
|
|
6
|
use File::Spec::Functions; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
138
|
|
10
|
1
|
|
|
1
|
|
6
|
use base qw/Mojolicious::Plugin/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
749
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Module implementation |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub register { |
16
|
1
|
|
|
1
|
1
|
42
|
my ( $self, $app, $conf ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
|
|
31
|
my $home = $app->home; |
19
|
1
|
50
|
|
|
|
43
|
my $cache_dir |
20
|
|
|
|
|
|
|
= -e $app->home->rel_dir('public') |
21
|
|
|
|
|
|
|
? $app->home->rel_dir('public') |
22
|
|
|
|
|
|
|
: $home->to_string; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#if given as an option |
25
|
1
|
50
|
|
|
|
146
|
if ( defined $conf->{cache_directory} ) { |
26
|
0
|
|
|
|
|
0
|
$cache_dir = $conf->{cache_directory}; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
4
|
my $actions; |
30
|
1
|
50
|
|
|
|
4
|
if ( defined $conf->{actions} ) { |
31
|
0
|
|
|
|
|
0
|
$actions = { map { $_ => 1 } @{ $conf->{actions} } }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$app->hook( |
35
|
|
|
|
|
|
|
'after_dispatch' => sub { |
36
|
0
|
|
|
0
|
|
0
|
my ($c) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
## - has to be GET request |
39
|
0
|
0
|
|
|
|
0
|
return if $c->req->method ne 'GET'; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
## - only successful response |
42
|
0
|
0
|
|
|
|
0
|
return if $c->res->code != 200; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
## - only html response |
45
|
0
|
0
|
|
|
|
0
|
return if $c->res->headers->content_type !~ /^text\/html/; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
## - have to match the action |
48
|
0
|
|
|
|
|
0
|
my $name = $c->stash('action'); |
49
|
|
|
|
|
|
|
return |
50
|
0
|
0
|
0
|
|
|
0
|
if defined $conf->{actions} |
51
|
|
|
|
|
|
|
and not exists $actions->{$name}; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
0
|
my $parts = $c->req->url->path->parts; |
54
|
0
|
|
|
|
|
0
|
my $file_name; |
55
|
0
|
0
|
|
|
|
0
|
if ( @$parts == 1 ) { |
56
|
0
|
|
|
|
|
0
|
$file_name = catfile( $cache_dir, $parts->[0] . '.html' ); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
0
|
|
|
|
|
0
|
my $end = pop @$parts; |
60
|
0
|
|
|
|
|
0
|
my $folder = $cache_dir; |
61
|
0
|
|
|
|
|
0
|
$folder = catdir( $folder, $_ ) for @$parts; |
62
|
0
|
|
|
|
|
0
|
make_path($folder); |
63
|
0
|
|
|
|
|
0
|
$file_name = catfile( $folder, $end . '.html' ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
$app->log->debug( |
67
|
|
|
|
|
|
|
"storing in cache for action **$name** in file $file_name"); |
68
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
0
|
my $handler = IO::File->new( $file_name, 'w' ) |
70
|
|
|
|
|
|
|
or croak "cannot create file:$!"; |
71
|
0
|
|
|
|
|
0
|
$handler->print( $c->res->body ); |
72
|
0
|
|
|
|
|
0
|
$handler->close; |
73
|
|
|
|
|
|
|
} |
74
|
1
|
|
|
|
|
10
|
); |
75
|
1
|
|
|
|
|
41
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# ABSTRACT: Page caching plugin |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__END__ |