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