lib/AI/MicroStructure/Memd.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 90 CC 8 SCAR 33.2
CC
8
Cov
0%
new
56.0
check
6.0
decruft
2.0
CRAP 27.7
1 #!/usr/bin/perl -W
2 package AI::MicroStructure::Memd;
3  
4 use warnings;
5 use strict;
6  
7  
8 use Cache::Memcached::Fast;
9 use Storable;
10 #use IO::Compress::Gzip;
11 #use IO::Uncompress::Gunzip;
12  
13 our (@addr, %params, $memd, $version_str, $version_num, $error);
14  
15  
16 sub new {
17  
18 my $class = shift;
19  
20 no strict 'refs';
21 my $self = bless { @_,storage=> AI::MicroStructure::Cache->new,
22 cache=>{},
23 dominant => [] }, $class;
24  
25  
26 # Use differently spelled host addresses to enable Ketama to hash
27 # names differently. Note that not all hosts resolve 127.x.y.z
28 # other than 127.0.0.1.
29 @addr = (
30 { address => 'localhost:11211', weight => 1.5 },
31 '127.0.0.1:11211',
32 # { address => '127.0.0.2:11211' },
33 # [ '127.0.0.3:11211', 2 ]
34 );
35  
36 %params = (
37 servers => [ @addr ],
38 namespace => "Cache::Memcached::Fast/$$/",
39 connect_timeout => 5,
40 io_timeout => 5,
41 close_on_error => 0,
42 compress_threshold => 1000,
43 # compress_methods => [ \&IO::Compress::Gzip::gzip,
44 # \&IO::Uncompress::Gunzip::gunzip ],
45 max_failures => 3,
46 failure_timeout => 2,
47 ketama_points => 150,
48 nowait => 1,
49 serialize_methods => [ \&Storable::freeze, \&Storable::thaw ],
50 utf8 => ($^V ge v5.8.1 ? 1 : 0),
51 );
52  
53 $memd = Cache::Memcached::Fast->new(\%params);
54  
55 # Test what server version we have. server_versions() is
56 # currently undocumented. We know that all servers are the same,
57 # so test only the first version.
58 my $version = $memd->server_versions;
59 if (keys %$version == @addr) {
60 $version_num = 2 ** 31;
61 while (my ($s, $v) = each %$version) {
62 if ($v =~ /(\d+)\.(\d+)\.(\d+)/) {
63 my $n = $1 * 10000 + $2 * 100 + $3;
64 if ($n < $version_num) {
65 $version_str = $v;
66 $version_num = $n;
67 }
68 } else {
69 $error = "Can't parse version of $s: $v";
70 undef $memd;
71 last;
72 }
73 }
74 } else {
75 my @servers = map {
76 if (ref($_) eq 'HASH') {
77 $_->{address};
78 } elsif (ref($_) eq 'ARRAY') {
79 $_->[0];
80 } else {
81 $_;
82 }
83 } @addr;
84  
85 $error = "No server is running at "
86 . join(', ', grep { not exists $version->{$_} } @servers);
87 undef $memd;
88 }
89  
90  
91 return $memd;
92 }
93  
94  
95  
96 sub decruft {
97 my $self = shift;
98 my($file) = @_;
99 my($cruftSet) = q{%§&|#[^+*( ]),'"; };
100 my $clean = $file;
101 $clean=~s/\Q$_//g for split("",$cruftSet);
102  
103 return $clean;
104 }
105  
106 sub check {
107 my $self = shift;
108 my $name = shift;
109 my $prog = shift;
110 $name = $self->decruft($name);
111  
112 if(defined(my $ret = $AI::MicroStructure::Memd::memd->get(sprintf("%s_%s",$name,$prog)))){
113 print "from cache";
114 return $ret;
115 }else{
116 my $ret = `$prog $name`;
117 $AI::MicroStructure::Memd::memd->set(sprintf("%s_%s",$name,$prog),$ret);
118 print "from set";
119 return $ret;
120 }
121 }
122  
123  
124 1;
125