line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CHI; |
2
|
6
|
|
|
6
|
|
3602
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
6
|
|
|
|
|
19
|
|
|
6
|
|
|
|
|
47
|
|
3
|
6
|
|
|
6
|
|
1039
|
use Scalar::Util 'weaken'; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
261
|
|
4
|
6
|
|
|
6
|
|
2480
|
use CHI; |
|
6
|
|
|
|
|
320660
|
|
|
6
|
|
|
|
|
2841
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.19'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Register Plugin |
9
|
|
|
|
|
|
|
sub register { |
10
|
10
|
|
|
10
|
1
|
20905
|
my ($plugin, $mojo, $param) = @_; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Load parameter from config file |
13
|
10
|
100
|
|
|
|
46
|
if (my $config_param = $mojo->config('CHI')) { |
14
|
5
|
|
|
|
|
73
|
$param = { %$param, %$config_param }; |
15
|
|
|
|
|
|
|
}; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Hash of cache handles |
18
|
10
|
|
|
|
|
84
|
my $caches; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Add 'chi_handles' attribute |
21
|
|
|
|
|
|
|
# Necessary for multiple cache registrations |
22
|
10
|
100
|
|
|
|
59
|
unless ($mojo->can('chi_handles')) { |
23
|
|
|
|
|
|
|
$mojo->attr( |
24
|
|
|
|
|
|
|
chi_handles => sub { |
25
|
2
|
|
50
|
2
|
|
18
|
return ($caches //= {}); |
26
|
|
|
|
|
|
|
} |
27
|
6
|
|
|
|
|
58
|
); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Get caches from application |
31
|
|
|
|
|
|
|
else { |
32
|
4
|
|
|
|
|
13
|
$caches = $mojo->chi_handles; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Support classes |
36
|
10
|
|
100
|
|
|
298
|
my $chi_class = delete $param->{chi_class} // 'CHI'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Support namespaces |
39
|
10
|
|
100
|
|
|
42
|
my $ns = delete $param->{namespaces} // 1; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Create log callback for CHI Logging |
42
|
10
|
|
|
|
|
41
|
my $log = $mojo->log; |
43
|
10
|
|
|
|
|
825
|
weaken $log; |
44
|
|
|
|
|
|
|
my $log_ref = sub { |
45
|
1
|
50
|
|
1
|
|
655
|
$log->warn( shift ) if defined $log; |
46
|
10
|
|
|
|
|
39
|
}; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Loop through all caches |
49
|
10
|
|
|
|
|
30
|
foreach my $name (keys %$param) { |
50
|
17
|
|
|
|
|
35
|
my $cache_param = $param->{$name}; |
51
|
|
|
|
|
|
|
|
52
|
17
|
50
|
33
|
|
|
90
|
next unless ref $cache_param && ref $cache_param eq 'HASH'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Already exists |
55
|
17
|
100
|
|
|
|
44
|
if (exists $caches->{$name}) { |
56
|
4
|
|
|
|
|
13
|
$mojo->log->warn(qq{Multiple attempts to establish cache "$name"}); |
57
|
4
|
|
|
|
|
130
|
next; |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Set namespace |
61
|
13
|
100
|
|
|
|
38
|
if ($ns) { |
62
|
12
|
100
|
33
|
|
|
64
|
$cache_param->{namespace} //= $name unless $name eq 'default'; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Get CHI handle |
66
|
13
|
|
|
|
|
74
|
my $cache = $chi_class->new( |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Set logging routines |
69
|
|
|
|
|
|
|
on_get_error => $log_ref, |
70
|
|
|
|
|
|
|
on_set_error => $log_ref, |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
%$cache_param |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# No succesful creation |
76
|
13
|
50
|
|
|
|
516817
|
$mojo->log->warn(qq{Unable to create cache handle "$name"}) unless $cache; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Store CHI handle |
79
|
13
|
|
|
|
|
45
|
$caches->{$name} = $cache; |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Only establish once |
83
|
10
|
100
|
|
|
|
77
|
unless (exists $mojo->renderer->helpers->{chi}) { |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Add 'chi' command |
86
|
6
|
|
|
|
|
127
|
push @{$mojo->commands->namespaces}, __PACKAGE__; |
|
6
|
|
|
|
|
46
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Add 'chi' helper |
89
|
|
|
|
|
|
|
$mojo->helper( |
90
|
|
|
|
|
|
|
chi => sub { |
91
|
46
|
|
|
46
|
|
27210
|
my $c = shift; |
92
|
46
|
|
100
|
|
|
141
|
my $name = shift // 'default'; |
93
|
|
|
|
|
|
|
|
94
|
46
|
|
|
|
|
106
|
my $cache = $caches->{$name}; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Cache unknown |
97
|
46
|
100
|
|
|
|
143
|
$c->app->log->warn(qq{Unknown cache handle "$name"}) unless $cache; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Return cache |
100
|
46
|
|
|
|
|
264
|
return $cache; |
101
|
|
|
|
|
|
|
} |
102
|
6
|
|
|
|
|
317
|
); |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |