PHP 中memcache连接

php memcache模块版本:

php –re memcache
Extension [ <persistent> extension #28 memcache version <no_version> ] {

今天使用memcache时,发现这么一个问题,使用同一个mc_client对象,依次连接多个memcached,第二个memcached确实进行了连接,但是getStats()时仍然使用的是第一个memcached的连接,如果要依次连接不同的memcached则需要重新new Memcache();
下面是测试脚本

<?php
$arrHosts
= array(‘10.10.10.11’=>‘11211’,‘10.10.10.12’=>‘11211’);

$mc = new Memcache();
echo
serialize($mc)."\n";
foreach(
$arrHosts as $host=>$port) {
echo
‘start ‘.$host."\n";
$mc->connect($host,$port);
echo
serialize($mc)."\n";
$stats = $mc->getStats();
echo
$stats[‘pid’]."\n";
sleep(10);
}
?>

面是strace的部分调用:观察发现两次connect使用的是不同的fd,而send却使用的是同一个fd

1054 fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1),}) = 0
1055 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 1, 0) = 0xb7de9000
1056 write(1, "start 10.10.10.11\n", 18) = 18
1057 socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 3
1058 close(3) = 0
1059 gettimeofday({1223351084, 844527}, NULL) = 0
1060 socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
1061 fcntl64(3, F_GETFL) = 0x2 (flags O_RDWR)
1062 fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
1063 connect(3, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr("10.10.10.11")}, 16) = -1 EINPROGRESS (Operation now in progress)
1064 poll([{fd=3, events=POLLIN|POLLOUT|POLLERR|POLLHUP, revents=POLLOUT}], 1, 1000) = 1
1065 getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
1066 fcntl64(3, F_SETFL, O_RDWR) = 0
1067 send(3, "stats\r\n", 7, 0) = 7
1068 poll([{fd=3, events=POLLIN|POLLERR|POLLHUP, revents=POLLIN}], 1, 1000) = 1
1069 recv(3, "STAT pid 15727\r\nSTAT uptime 1293", 8192, 0) = 555
1070 write(1, "15727\n", 6) = 6
1071 write(1, "start 10.10.10.12\n", 18) = 18
1072 gettimeofday({1223351084, 845984}, NULL) = 0
1073 socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 4
1074 fcntl64(4, F_GETFL) = 0x2 (flags O_RDWR)
1075 fcntl64(4, F_SETFL, O_RDWR|O_NONBLOCK) = 0
1076 connect(4, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr("10.10.10.12")}, 16) = -1 EINPROGRESS (Operation now in progress)
1077 poll([{fd=4, events=POLLIN|POLLOUT|POLLERR|POLLHUP, revents=POLLOUT}], 1, 1000) = 1
1078 getsockopt(4, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
1079 fcntl64(4, F_SETFL, O_RDWR) = 0
1080 send(3, "stats\r\n", 7, 0) = 7
1081 poll([{fd=3, events=POLLIN|POLLERR|POLLHUP, revents=POLLIN}], 1, 1000) = 1
1082 recv(3, "STAT pid 15727\r\nSTAT uptime 1293", 8192, 0) = 555
1083 write(1, "15727\n", 6) = 6
1084 close(3) = 0
1085 close(4) = 0
1086 close(0
) = 0

下面的php memcache模块是没有问题的

Extension [ <persistent> extension #26 memcache version 2.2.4 ] {

所以,使用php memcache模块时注意版本号哦,

留下评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据