| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::CHI::chi; | 
| 2 | 1 |  |  | 1 |  | 29858 | use Mojo::Base 'Mojolicious::Command'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 21 |  | 
| 3 | 1 |  |  | 1 |  | 229 | use Mojo::Util qw/tablify quote/; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 68 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 7 | use Getopt::Long qw/GetOptions :config no_auto_abbrev no_ignore_case/; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 16 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | has description => 'Interact with CHI caches'; | 
| 8 |  |  |  |  |  |  | has usage       => sub { shift->extract_usage }; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub _unknown { | 
| 11 | 4 |  |  | 4 |  | 17 | return 'Unknown cache handle ' . quote($_[0]) . ".\n\n" | 
| 12 |  |  |  |  |  |  | }; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # Run chi | 
| 15 |  |  |  |  |  |  | sub run { | 
| 16 | 18 |  |  | 18 | 1 | 531172 | my $self = shift; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 18 |  |  |  |  | 43 | my $command = shift; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 18 | 100 | 50 |  |  | 65 | print $self->usage and return unless $command; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # Get the application | 
| 23 | 16 |  |  |  |  | 77 | my $app = $self->app; | 
| 24 | 16 |  |  |  |  | 127 | my $log = $app->log; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # List all associated caches | 
| 27 | 16 | 100 | 100 |  |  | 199 | if ($command eq 'list') { | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 28 | 2 |  |  |  |  | 11 | my $caches = $app->chi_handles; | 
| 29 | 2 |  |  |  |  | 7 | my @list; | 
| 30 | 2 |  |  |  |  | 18 | foreach (sort { lc($a) cmp lc($b) } keys %$caches) { | 
|  | 3 |  |  |  |  | 14 |  | 
| 31 | 5 |  | 50 |  |  | 237 | push(@list, [$_, ($caches->{$_}->short_driver_name || '[UNKNOWN]')]); | 
| 32 |  |  |  |  |  |  | }; | 
| 33 | 2 |  |  |  |  | 42 | print tablify \@list; | 
| 34 | 2 |  |  |  |  | 209 | return 1; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | # Purge or clear a cache | 
| 38 |  |  |  |  |  |  | elsif ($command eq 'purge' || $command eq 'clear') { | 
| 39 | 6 |  | 100 |  |  | 25 | my $cache = shift || 'default'; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 6 |  |  |  |  | 56 | my $chi = $app->chi($cache); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # Cache is unknown | 
| 44 | 6 | 100 | 50 |  |  | 38 | print _unknown($cache) and return unless $chi; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # Do not modify non-persistant in-process caches! | 
| 47 | 4 | 50 |  |  |  | 137 | 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 |  |  |  |  | 157 | $chi->$command(); | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # Purge or clear cache | 
| 56 | 4 | 100 |  |  |  | 6132 | print qq{Cache "$cache" was } . $command . | 
| 57 |  |  |  |  |  |  | ($command eq 'clear' ? 'ed' : 'd') . ".\n\n"; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 4 |  |  |  |  | 34 | return 1; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Remove or expire a key | 
| 63 |  |  |  |  |  |  | elsif ($command eq 'remove' || $command eq 'expire') { | 
| 64 | 8 |  |  |  |  | 19 | my $key   = pop(@_); | 
| 65 | 8 |  | 100 |  |  | 24 | my $cache = shift || 'default'; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 8 | 50 |  |  |  | 20 | if ($key) { | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 8 |  |  |  |  | 46 | my $chi = $app->chi($cache); | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # Cache is unknown | 
| 72 | 8 | 100 | 50 |  |  | 39 | print _unknown($cache) and return unless $chi;; | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # Do not modify non-persistant in-process caches! | 
| 75 | 6 | 50 |  |  |  | 146 | 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 |  |  |  | 151 | if ($chi->$command($key)) { | 
| 84 | 4 |  |  |  |  | 1853 | print qq{Key "$key" from cache "$cache" was } . $command . "d.\n\n"; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # Not successful | 
| 88 |  |  |  |  |  |  | else { | 
| 89 | 2 |  |  |  |  | 382 | print 'Unable to ' . $command . | 
| 90 |  |  |  |  |  |  | qq{ key "$key" from cache "$cache".\n\n}; | 
| 91 |  |  |  |  |  |  | }; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 6 |  |  |  |  | 47 | 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__ |