PHP中使用 Memcached 的测试案例

  1 <?php
  2 
  3 class MemcacheController extends ControllerBase
  4 {
  5     public function indexAction()
  6     {
  7         session_start();
  8         $sessid = session_id();
  9         
 10         $server = array(
 11             array('localhost',11200),
 12             array('localhost',11201),
 13             array('localhost',11202),
 14         );
 15         $memCache = new Memcached('conn_pool');
 16         $curt_server = $this->connectMemcache($sessid,$memCache,$server);
 17         echo "<br><br>目前连接的服务器:".json_encode($curt_server);
 18         if(!empty($curt_server)){
 19             //设置60秒过期
 20             $issetm = $memCache->set("user",$sessid,3600);     
 21             if($issetm)
 22             {
 23                 echo "<br>设置memcached user成功!";
 24             }
 25         }               
 26     }
 27     public function getAction(){
 28         session_start();
 29         $sessid = session_id();
 30         $server = array(
 31             array('localhost',11200),
 32             array('localhost',11201),
 33             array('localhost',11202),
 34         );
 35         $memCache = new Memcached('conn_pool');
 36         $curt_server = $this->connectMemcache($sessid,$memCache,$server);
 37         echo "<br><br>目前连接的服务器:".json_encode($curt_server);
 38         $user_sid = $memCache->get("user");
 39         echo "sessionid=【{$sessid}】  获取memcached数据:".$user_sid;
 40         echo "<br>";
 41         //根据当前sessid 和 取出的 user的数据比较;不相同重新登录
 42         if($sessid != $user_sid){
 43             echo "////////重新登录///////";
 44             $issetm = $memCache->set("user",$sessid,3600);
 45             if($issetm)
 46             {
 47                 echo "<br>设置sessionid memcached user成功!";
 48             }
 49         }
 50     }
 51     private function connectMemcache( $sessid , $memCache , $server= array()){
 52         $server_num = count($server);
 53         
 54         //获取[memcached]服务器
 55         $laststr = substr($sessid, -1);
 56         $curr_idx = $this->bcd2char(strtolower($laststr)) % $server_num;
 57         $host = $server[$curr_idx][0];
 58         $port = $server[$curr_idx][1];
 59         $serv = $host . ':' . $port;        //预期连接的台服务器[对应: $sessid]
 60         echo "<br><br><br>------------------开始连接第【{$curr_idx}】台server主机={$host} 端口={$port} 预期连接的地址:{$serv}----------------<br/>";
 61         $serv_arr = $memCache->getServerList(); 
 62         $list_cnt = count($serv_arr);
 63         echo "当前活动的Memcached 总数:{$list_cnt}个 活动的 服务IP是:".json_encode($serv_arr)."<br>";
 64         if($list_cnt>0){
 65             //如果存在;判断状态
 66             $stat_arr = $memCache->getStats();
 67             //获取获取服务器池中所有活动的服务器
 68             $stat_ver = $memCache->getVersion();
 69             if($stat_arr){
 70                 foreach ($stat_arr as $keyhost => $stat){
 71                     if ($stat['pid'] > 0) {         //如果存在活动的;判断服务器的进程(pid >0)时, 表示该服务器正常!
 72                         if ($keyhost == $serv) {
 73                             //如果访问ip与预期的ip相同返回当前ip
 74                             echo "已经找到该活动进程ID={$stat['pid']}服务器 版本:".json_encode($stat_ver)."<br>";
 75                             return $server[$curr_idx];
 76                         }
 77                     }
 78                 }
 79             }
 80         }
 81         //如果为匹配ip;则清除服务器列表中的所有服务器;重新添加
 82         $resetmem = $memCache->resetServerList();
 83         if($resetmem){
 84             echo "+++++++++++++++清除服务器列表中的所有服务器;开始重新添加服务器列表Success.+++++++++++++++ <br/>";
 85         }else{
 86             echo "+++++++++++++++清除服务器列表中的所有服务器Faild+++++++++++++++.<br/>";
 87         }
 88         //重新连接
 89         if ($memCache->addServer($host, $port)) {   /* 短连接模式 */
 90             echo "重新添加服务器第【{$curr_idx}】台server主机={$host} 端口={$port}<br/>";
 91             //如果存在;判断状态
 92             $stat_arr = $memCache->getStats();
 93             //获取获取服务器池中所有服务器的版本信息
 94             $stat_ver = $memCache->getVersion();
 95             if($stat_arr){
 96                 echo "<b><font color=green>重新添加服务器成功;服务器版本:".json_encode($stat_ver)."</font></b><br>";
 97                 return $server[$curr_idx];
 98             }else{
 99                 echo "=================================添加状态失败 server主机={$host} 端口={$port}=================================<br>";
100                 //删去失败的[memcached]配置
101                 $temp = $server[$curr_idx];
102                 unset($server[$curr_idx]);                
103                 $resetmem = $memCache->resetServerList();//还原服务器列表
104                 if(count($server)>0 && $resetmem){
105                     sort($server);
106                     echo "<b><font color=red>【开始删除】连接失败的服务器:{$temp[0]}:{$temp[1]} *****重试其他的服务列表:".json_encode($server)."</font><b><br>";
107                     return $this->connectMemcache($sessid , $memCache , $server);
108                 }else{
109                     echo "No valid memcached servers!";
110                     return false;
111                 }
112             }
113         }
114     }
115     /*
116      * BCD到字符码
117      * @param $char 字符码到BCD
118      * @return 字符码
119      */
120     private function bcd2char($char)
121     {
122         $bcd = ord($char);
123         if ($bcd >= 97 && $bcd <= 102) {
124             //'a'..'f'
125             return $bcd - 87;
126         } else {
127             //'0'..'9'
128             return $bcd - 48;
129         }
130     }
131     
132 }

 

目标主机:localhost:11200 | localhost:11201 | localhost:11202

根据user客户端的sessionid 来定位使用哪台服务器;如果其中一台挂掉;则;循环添加其他的memcache;直到添加成功返回;连接的当前主机IP;

运行结果:

 

 

posted @ 2018-04-12 16:38  王默默  阅读(424)  评论(0编辑  收藏  举报