| 1 | ########################################### | ||||||||||
| 2 | package IPC::Cmd::Cached; | ||||||||||
| 3 | ########################################### | ||||||||||
| 4 | # 2007, Mike Schilli <cpan@perlmeister.com> | ||||||||||
| 5 | ########################################### | ||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Cache::FileCache; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use IPC::Cmd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Storable qw(freeze thaw); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Log::Log4perl qw(:easy); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | our $VERSION = "0.01"; | |||||||||
| 15 | |||||||||||
| 16 | ########################################### | ||||||||||
| 17 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||
| 18 | ########################################### | ||||||||||
| 19 | 1 | my($class, %options) = @_; | |||||||||
| 20 | |||||||||||
| 21 | 1 | my $self = { | |||||||||
| 22 | cache => undef, | ||||||||||
| 23 | %options, | ||||||||||
| 24 | }; | ||||||||||
| 25 | |||||||||||
| 26 | 1 | ▶ | if(! defined $self->{cache}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | $self->{cache} = Cache::FileCache->new({ | |||||||||
| 28 | auto_purge_on_get => 1, | ||||||||||
| 29 | default_expires_in => 24*3600, | ||||||||||
| 30 | namespace => "IPC-Cmd-Cached", | ||||||||||
| 31 | }); | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 1 | bless $self, $class; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | ########################################### | ||||||||||
| 38 | 2 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||
| 39 | ########################################### | ||||||||||
| 40 | 2 | my($self, @opts) = @_; | |||||||||
| 41 | |||||||||||
| 42 | 2 | DEBUG "Running @opts"; | |||||||||
| 43 | |||||||||||
| 44 | 2 | my @result = IPC::Cmd::run(@opts); | |||||||||
| 45 | |||||||||||
| 46 | 2 | ▶ | if(! defined $result[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | ERROR $result[4]->[0]; | |||||||||
| 48 | 0 | return undef; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 2 | DEBUG "Return: $result[0]\n"; | |||||||||
| 52 | |||||||||||
| 53 | 2 | ▶ | if($result[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 2 | DEBUG "Stdout: ", join('', @{$result[3]}); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | my $data = freeze({ | |||||||||
| 58 | result => \@result, | ||||||||||
| 59 | opts => \@opts, | ||||||||||
| 60 | time => time, | ||||||||||
| 61 | }); | ||||||||||
| 62 | |||||||||||
| 63 | 2 | my $key = normalize(@opts); | |||||||||
| 64 | |||||||||||
| 65 | 2 | $self->{cache}->set($key, $data); | |||||||||
| 66 | |||||||||||
| 67 | 2 | return @result; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | ########################################### | ||||||||||
| 71 | 2 | ▶ | sub run_cached { | ||||||||
|
Subroutine
run_cached: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 72 | ########################################### | ||||||||||
| 73 | 2 | my($self, @opts) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 2 | my $key = normalize(@opts); | |||||||||
| 76 | |||||||||||
| 77 | 2 | my $stored = $self->{cache}->get($key); | |||||||||
| 78 | |||||||||||
| 79 | 2 | ▶ | if(defined $stored) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 2 | DEBUG "Found result for @opts in cache"; | |||||||||
| 81 | 2 | my $data = thaw($stored); | |||||||||
| 82 | 2 | return @{ $data->{result} }; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | DEBUG "Didn't find result for @opts in cache"; | |||||||||
| 86 | 0 | return undef; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | ########################################### | ||||||||||
| 90 | 4 | ▶ | sub normalize { | ||||||||
|
Subroutine
normalize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 91 | ########################################### | ||||||||||
| 92 | 4 | my(@args) = @_; | |||||||||
| 93 | |||||||||||
| 94 | 4 | return join(" ", @args); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | ########################################### | ||||||||||
| 98 | 0 | ▶ | sub run_all { | ||||||||
|
Subroutine
run_all: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 99 | ########################################### | ||||||||||
| 100 | 0 | my($self) = @_; | |||||||||
| 101 | |||||||||||
| 102 | 0 | for my $key ($self->{cache}->get_keys()) { | |||||||||
| 103 | 0 | my $stored = $self->{cache}->get( $key ); | |||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | if(defined $stored) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 0 | my $data = thaw($stored); | |||||||||
| 107 | 0 | $self->run( @{ $data->{opts} } ); | |||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 1 | 1; | |||||||||
| 113 | |||||||||||
| 114 | __END__ | ||||||||||