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