line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CHI::chi; |
2
|
1
|
|
|
1
|
|
23310
|
use Mojo::Base 'Mojolicious::Command'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
15
|
|
3
|
1
|
|
|
1
|
|
162
|
use Mojo::Util qw/tablify quote/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
49
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use Getopt::Long qw/GetOptions :config no_auto_abbrev no_ignore_case/; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
9
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has description => 'Interact with CHI caches'; |
8
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage }; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub _unknown { |
11
|
4
|
|
|
4
|
|
13
|
return 'Unknown cache handle ' . quote($_[0]) . ".\n\n" |
12
|
|
|
|
|
|
|
}; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Run chi |
15
|
|
|
|
|
|
|
sub run { |
16
|
18
|
|
|
18
|
1
|
502813
|
my $self = shift; |
17
|
|
|
|
|
|
|
|
18
|
18
|
|
|
|
|
26
|
my $command = shift; |
19
|
|
|
|
|
|
|
|
20
|
18
|
100
|
50
|
|
|
54
|
print $self->usage and return unless $command; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Get the application |
23
|
16
|
|
|
|
|
58
|
my $app = $self->app; |
24
|
16
|
|
|
|
|
104
|
my $log = $app->log; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# List all associated caches |
27
|
16
|
100
|
100
|
|
|
146
|
if ($command eq 'list') { |
|
|
100
|
66
|
|
|
|
|
|
|
50
|
|
|
|
|
|
28
|
2
|
|
|
|
|
7
|
my $caches = $app->chi_handles; |
29
|
2
|
|
|
|
|
6
|
my @list; |
30
|
2
|
|
|
|
|
13
|
foreach (sort { lc($a) cmp lc($b) } keys %$caches) { |
|
3
|
|
|
|
|
13
|
|
31
|
5
|
|
50
|
|
|
205
|
push(@list, [$_, ($caches->{$_}->short_driver_name || '[UNKNOWN]')]); |
32
|
|
|
|
|
|
|
}; |
33
|
2
|
|
|
|
|
28
|
print tablify \@list; |
34
|
2
|
|
|
|
|
167
|
return 1; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Purge or clear a cache |
38
|
|
|
|
|
|
|
elsif ($command eq 'purge' || $command eq 'clear') { |
39
|
6
|
|
100
|
|
|
16
|
my $cache = shift || 'default'; |
40
|
|
|
|
|
|
|
|
41
|
6
|
|
|
|
|
40
|
my $chi = $app->chi($cache); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Cache is unknown |
44
|
6
|
100
|
50
|
|
|
25
|
print _unknown($cache) and return unless $chi; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Do not modify non-persistant in-process caches! |
47
|
4
|
50
|
|
|
|
87
|
if ($chi->short_driver_name =~ /^(?:Raw)?Memory$/) { |
48
|
0
|
|
|
|
|
0
|
$log->warn("You are trying to $command a ". |
49
|
|
|
|
|
|
|
$chi->short_driver_name . |
50
|
|
|
|
|
|
|
'-Cache'); |
51
|
|
|
|
|
|
|
}; |
52
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
83
|
$chi->$command(); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Purge or clear cache |
56
|
4
|
100
|
|
|
|
4601
|
print qq{Cache "$cache" was } . $command . |
57
|
|
|
|
|
|
|
($command eq 'clear' ? 'ed' : 'd') . ".\n\n"; |
58
|
|
|
|
|
|
|
|
59
|
4
|
|
|
|
|
24
|
return 1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Remove or expire a key |
63
|
|
|
|
|
|
|
elsif ($command eq 'remove' || $command eq 'expire') { |
64
|
8
|
|
|
|
|
14
|
my $key = pop(@_); |
65
|
8
|
|
100
|
|
|
21
|
my $cache = shift || 'default'; |
66
|
|
|
|
|
|
|
|
67
|
8
|
50
|
|
|
|
13
|
if ($key) { |
68
|
|
|
|
|
|
|
|
69
|
8
|
|
|
|
|
51
|
my $chi = $app->chi($cache); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Cache is unknown |
72
|
8
|
100
|
50
|
|
|
31
|
print _unknown($cache) and return unless $chi;; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Do not modify non-persistant in-process caches! |
75
|
6
|
50
|
|
|
|
126
|
if ($chi->short_driver_name =~ /^(?:Raw)?Memory$/) { |
76
|
0
|
|
|
|
|
0
|
$log->warn("You are trying to $command " . |
77
|
|
|
|
|
|
|
'a key from a '. |
78
|
|
|
|
|
|
|
$chi->short_driver_name . |
79
|
|
|
|
|
|
|
'-Cache'); |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Remove or expire key |
83
|
6
|
100
|
|
|
|
152
|
if ($chi->$command($key)) { |
84
|
4
|
|
|
|
|
1754
|
print qq{Key "$key" from cache "$cache" was } . $command . "d.\n\n"; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Not successful |
88
|
|
|
|
|
|
|
else { |
89
|
2
|
|
|
|
|
341
|
print 'Unable to ' . $command . |
90
|
|
|
|
|
|
|
qq{ key "$key" from cache "$cache".\n\n}; |
91
|
|
|
|
|
|
|
}; |
92
|
|
|
|
|
|
|
|
93
|
6
|
|
|
|
|
34
|
return 1; |
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Unknown command |
98
|
0
|
0
|
|
|
|
|
print $self->usage and return; |
99
|
|
|
|
|
|
|
}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__END__ |