File Coverage

blib/lib/IPC/Cmd/Cached.pm
Criterion Covered Total %
statement 44 56 78.5
branch 4 10 40.0
condition n/a
subroutine 10 11 90.9
pod 0 5 0.0
total 58 82 70.7


line stmt bran cond sub pod time code
1             ###########################################
2             package IPC::Cmd::Cached;
3             ###########################################
4             # 2007, Mike Schilli
5             ###########################################
6              
7 1     1   60892 use strict;
  1         4  
  1         40  
8 1     1   6 use warnings;
  1         3  
  1         29  
9 1     1   1497 use Cache::FileCache;
  1         127058  
  1         60  
10 1     1   5815 use IPC::Cmd;
  1         115581  
  1         44  
11 1     1   11 use Storable qw(freeze thaw);
  1         2  
  1         49  
12 1     1   1749 use Log::Log4perl qw(:easy);
  1         59665  
  1         9  
13              
14             our $VERSION = "0.01";
15              
16             ###########################################
17             sub new {
18             ###########################################
19 1     1 0 1368 my($class, %options) = @_;
20              
21 1         6 my $self = {
22             cache => undef,
23             %options,
24             };
25              
26 1 50       6 if(! defined $self->{cache}) {
27 0         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         5 bless $self, $class;
35             }
36              
37             ###########################################
38             sub run {
39             ###########################################
40 2     2 0 634 my($self, @opts) = @_;
41              
42 2         20 DEBUG "Running @opts";
43              
44 2         36 my @result = IPC::Cmd::run(@opts);
45              
46 2 50       64150 if(! defined $result[0]) {
47 0         0 ERROR $result[4]->[0];
48 0         0 return undef;
49             }
50              
51 2         53 DEBUG "Return: $result[0]\n";
52              
53 2 50       48 if($result[0]) {
54 2         4 DEBUG "Stdout: ", join('', @{$result[3]});
  2         16  
55             }
56              
57 2         71 my $data = freeze({
58             result => \@result,
59             opts => \@opts,
60             time => time,
61             });
62              
63 2         438 my $key = normalize(@opts);
64              
65 2         121 $self->{cache}->set($key, $data);
66              
67 2         7020 return @result;
68             }
69              
70             ###########################################
71             sub run_cached {
72             ###########################################
73 2     2 0 3321 my($self, @opts) = @_;
74              
75 2         8 my $key = normalize(@opts);
76              
77 2         54 my $stored = $self->{cache}->get($key);
78              
79 2 50       1531 if(defined $stored) {
80 2         19 DEBUG "Found result for @opts in cache";
81 2         30 my $data = thaw($stored);
82 2         44 return @{ $data->{result} };
  2         17  
83             }
84              
85 0         0 DEBUG "Didn't find result for @opts in cache";
86 0         0 return undef;
87             }
88              
89             ###########################################
90             sub normalize {
91             ###########################################
92 4     4 0 21 my(@args) = @_;
93              
94 4         14 return join(" ", @args);
95             }
96              
97             ###########################################
98             sub run_all {
99             ###########################################
100 0     0 0   my($self) = @_;
101              
102 0           for my $key ($self->{cache}->get_keys()) {
103 0           my $stored = $self->{cache}->get( $key );
104              
105 0 0         if(defined $stored) {
106 0           my $data = thaw($stored);
107 0           $self->run( @{ $data->{opts} } );
  0            
108             }
109             }
110             }
111              
112             1;
113              
114             __END__