perl如何将数据复制到多个Memcached服务器上面:
#!/usr/bin/perl
use Cache::Memcached;
my $con_ref;
my $servers= [
'127.0.0.1:11221',
'127.0.0.1:11222',
'127.0.0.1:11223',
'127.0.0.1:11224',
'127.0.0.1:11225' ];
for my $server (@$servers) {
$con_aref->{$server} = new Cache::Memcached({
servers => [ $server ],
compress_threshold => 10_000 });
}
my $main_con= new Cache::Memcached({
servers => $servers,
compress_threshold => 10_000});
my $value = "Test value";
my $key = "test:key1";
my $rc = multi_set($key, $value);
my $retval = $main_con->get($key);
print "retval: $retval\n";
sub multi_set {
my ($key, $value) = @_;
my $all_set= 0;
for my $server (@$servers) {
my $rc = $con_aref->{$server}->set($key, $value);
unless ($rc) {
print "ERROR: unable to set $key in server $server\n";
$all_set--;
}
$all_set++;
}
return ($all_set == scalar @$servers);
}