line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CachePurge; |
2
|
1
|
|
|
1
|
|
1081
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
11
|
|
3
|
1
|
|
|
1
|
|
241
|
use Mojo::URL; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
11
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has 'ua' => sub { Mojo::UserAgent->new; }; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub register { |
10
|
1
|
|
|
1
|
1
|
42
|
my ( $self, $app, $conf ) = @_; |
11
|
|
|
|
|
|
|
|
12
|
1
|
50
|
|
|
|
6
|
if ( defined( $conf->{baseurl} ) ) { |
13
|
0
|
0
|
|
|
|
0
|
$app->log->error("CachePurge: Can not parse baseurl as url, aborting") |
14
|
|
|
|
|
|
|
unless ( $self->{'baseurl'} = Mojo::URL->new( $conf->{'baseurl'} ) ); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
else { |
17
|
1
|
|
|
|
|
35
|
$app->log->error("CachePurge: No baseurl configured, aborting"); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
1
|
50
|
|
|
|
117
|
if ( $self->{'baseurl'} ) { |
21
|
0
|
|
|
|
|
0
|
$self->_add_log_hooks($app); |
22
|
0
|
|
|
0
|
|
0
|
$app->helper( 'cache_purge' => sub { $self->_purge(@_) } ); |
|
0
|
|
|
|
|
0
|
|
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
else { |
25
|
|
|
|
|
|
|
$app->helper( 'cache_purge' => |
26
|
1
|
|
|
0
|
|
11
|
sub { $app->log->debug('CachePurge: inactive, config error') } ); |
|
0
|
|
|
|
|
0
|
|
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
121
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _add_log_hooks { |
33
|
0
|
|
|
0
|
|
|
my ( $self, $app ) = @_; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$self->ua->on( |
36
|
|
|
|
|
|
|
start => sub { |
37
|
0
|
|
|
0
|
|
|
my ( $ua, $tx ) = @_; |
38
|
0
|
|
|
|
|
|
my $msg = |
39
|
|
|
|
|
|
|
"CachePurge: request, method=" |
40
|
|
|
|
|
|
|
. $tx->req->method |
41
|
|
|
|
|
|
|
. ", url=" |
42
|
|
|
|
|
|
|
. $tx->req->url; |
43
|
0
|
|
|
|
|
|
$app->log->debug($msg); |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
error => sub { |
46
|
0
|
|
|
0
|
|
|
my ( $ua, $err ) = @_; |
47
|
0
|
|
|
|
|
|
$app->log->error( "CachePurge: " . $err ); |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _purge { |
54
|
0
|
|
|
0
|
|
|
my ( $self, $c, $args, $cb ) = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# We may get a callback, but no args. It will be here as $args, so |
57
|
|
|
|
|
|
|
# we move it. |
58
|
0
|
0
|
|
|
|
|
if (ref $args eq 'CODE') { |
59
|
0
|
|
|
|
|
|
$cb = $args; |
60
|
0
|
|
|
|
|
|
undef $args; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
0
|
|
|
|
my $purge_path = $args->{'path'} ||= $c->req->url->path; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $purge_url = Mojo::URL->new( $self->{'baseurl'} )->path($purge_path); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Mojo::IOLoop->delay( |
68
|
|
|
|
|
|
|
sub { |
69
|
0
|
|
|
0
|
|
|
my ($delay) = @_; |
70
|
0
|
|
|
|
|
|
my $tx = $self->ua->build_tx( PURGE => $purge_url ); |
71
|
0
|
|
|
|
|
|
$self->ua->start( $tx => $delay->begin ); |
72
|
|
|
|
|
|
|
}, |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# log response |
75
|
|
|
|
|
|
|
sub { |
76
|
0
|
|
|
0
|
|
|
my ( $delay, $tx ) = @_; |
77
|
0
|
|
|
|
|
|
$c->app->log->debug( "CachePurge: response, url=" |
78
|
|
|
|
|
|
|
. $tx->req->url |
79
|
|
|
|
|
|
|
. ", code=" |
80
|
|
|
|
|
|
|
. $tx->res->code ); |
81
|
0
|
|
|
|
|
|
$delay->pass($tx); |
82
|
|
|
|
|
|
|
}, |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# handle callback, if any |
85
|
|
|
|
|
|
|
sub { |
86
|
0
|
|
|
0
|
|
|
my ( $delay, $tx ) = @_; |
87
|
0
|
0
|
|
|
|
|
$self->$cb($tx) if ($cb); |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
93
|
|
|
|
|
|
|
__END__ |