line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CHI; |
2
|
6
|
|
|
6
|
|
4627
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
6
|
|
|
|
|
34
|
|
|
6
|
|
|
|
|
57
|
|
3
|
6
|
|
|
6
|
|
1351
|
use Scalar::Util 'weaken'; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
332
|
|
4
|
6
|
|
|
6
|
|
3319
|
use CHI; |
|
6
|
|
|
|
|
380177
|
|
|
6
|
|
|
|
|
3729
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.18'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Register Plugin |
9
|
|
|
|
|
|
|
sub register { |
10
|
10
|
|
|
10
|
1
|
26019
|
my ($plugin, $mojo, $param) = @_; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Load parameter from config file |
13
|
10
|
100
|
|
|
|
59
|
if (my $config_param = $mojo->config('CHI')) { |
14
|
5
|
|
|
|
|
116
|
$param = { %$param, %$config_param }; |
15
|
|
|
|
|
|
|
}; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Hash of cache handles |
18
|
10
|
|
|
|
|
112
|
my $caches; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Add 'chi_handles' attribute |
21
|
|
|
|
|
|
|
# Necessary for multiple cache registrations |
22
|
10
|
100
|
|
|
|
79
|
unless ($mojo->can('chi_handles')) { |
23
|
|
|
|
|
|
|
$mojo->attr( |
24
|
|
|
|
|
|
|
chi_handles => sub { |
25
|
2
|
|
50
|
2
|
|
24
|
return ($caches //= {}); |
26
|
|
|
|
|
|
|
} |
27
|
6
|
|
|
|
|
61
|
); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Get caches from application |
31
|
|
|
|
|
|
|
else { |
32
|
4
|
|
|
|
|
17
|
$caches = $mojo->chi_handles; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Support classes |
36
|
10
|
|
100
|
|
|
407
|
my $chi_class = delete $param->{chi_class} // 'CHI'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Support namespaces |
39
|
10
|
|
100
|
|
|
48
|
my $ns = delete $param->{namespaces} // 1; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Create log callback for CHI Logging |
42
|
10
|
|
|
|
|
62
|
my $log = $mojo->log; |
43
|
10
|
|
|
|
|
1093
|
weaken $log; |
44
|
|
|
|
|
|
|
my $log_ref = sub { |
45
|
1
|
50
|
|
1
|
|
1465
|
$log->warn( shift ) if defined $log; |
46
|
10
|
|
|
|
|
47
|
}; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Loop through all caches |
49
|
10
|
|
|
|
|
41
|
foreach my $name (keys %$param) { |
50
|
17
|
|
|
|
|
44
|
my $cache_param = $param->{$name}; |
51
|
|
|
|
|
|
|
|
52
|
17
|
50
|
33
|
|
|
131
|
next unless ref $cache_param && ref $cache_param eq 'HASH'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Already exists |
55
|
17
|
100
|
|
|
|
54
|
if (exists $caches->{$name}) { |
56
|
4
|
|
|
|
|
16
|
$mojo->log->warn(qq{Multiple attempts to establish cache "$name"}); |
57
|
4
|
|
|
|
|
167
|
next; |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Set namespace |
61
|
13
|
100
|
|
|
|
56
|
if ($ns) { |
62
|
12
|
100
|
33
|
|
|
92
|
$cache_param->{namespace} //= $name unless $name eq 'default'; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Get CHI handle |
66
|
13
|
|
|
|
|
103
|
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
|
|
|
|
653906
|
$mojo->log->warn(qq{Unable to create cache handle "$name"}) unless $cache; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Store CHI handle |
79
|
13
|
|
|
|
|
59
|
$caches->{$name} = $cache; |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Only establish once |
83
|
10
|
100
|
|
|
|
120
|
unless (exists $mojo->renderer->helpers->{chi}) { |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Add 'chi' command |
86
|
6
|
|
|
|
|
164
|
push @{$mojo->commands->namespaces}, __PACKAGE__; |
|
6
|
|
|
|
|
59
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Add 'chi' helper |
89
|
|
|
|
|
|
|
$mojo->helper( |
90
|
|
|
|
|
|
|
chi => sub { |
91
|
46
|
|
|
46
|
|
38419
|
my $c = shift; |
92
|
46
|
|
100
|
|
|
184
|
my $name = shift // 'default'; |
93
|
|
|
|
|
|
|
|
94
|
46
|
|
|
|
|
154
|
my $cache = $caches->{$name}; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Cache unknown |
97
|
46
|
100
|
|
|
|
159
|
$c->app->log->warn(qq{Unknown cache handle "$name"}) unless $cache; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Return cache |
100
|
46
|
|
|
|
|
414
|
return $cache; |
101
|
|
|
|
|
|
|
} |
102
|
6
|
|
|
|
|
447
|
); |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |