memcached+Mysql(主从)

jackxiang 2010-1-24 14:11 | |
点击在新窗口中浏览此图片


/*
index.php   程序入口,用来构造sql(如查询,更新)
config.php  配置参数(memcache,mysql)
init.php    封装memcached操作(memcache连接,设置,获取)
mysqli.php  封闭mysql操作(mysql主连接,mysql从连接,mysql主更新,mysql从查询)
*/

index.php
<?php
require 'init.php';              //加载init.php
$mem = new Memcached;            //建立memcached对象,对象为$mem
/*
$mem->set('en_xx','bucuo',0,30); //测试连接memcached,用来调试很不错。
echo($mem->get('en_xx'));
$mem->set('cn_jjyy','wokao',0,30);
echo($mem->get('cn_jjyy'));
*/

require 'mysqli.php';
$sq = new Mysql;    
//下面这两个sql是人为构造的,这也可以做成一个接口                
$sql = "Select * from traffic";                  
//$sql = "insert into traffic(id,acct_mth,amount) values(14,14,46)";
//进行判断,如果sql语句中有头有select(不区分大小写)
if(preg_match ("/^select/i", $sql)){
    $mdsql = md5($sql);
    if(!$result=$mem->get('cn_'.$mdsql)){      //这里选择的是cn memcached,大家也可以根据需求在前面做个条件来将数据存取放入到另一个memcached中。
           $result = $sq->fetArray($sql); //查询是 从mysql
           foreach($result as $var){
                echo $var['amount'];
            }
           $mem->set('cn_'.$mdsql,$result,0,600); //添加到 名为cn的memcached 服务器
    }else{
            foreach($result as $var){
                echo $var['amount']."<br>";
          }
    }
}else{
    $sq->mquery($sql);     //更新是 主mysql
}

?>



config.php

<?php
$memcached = array(
     'cn'=>array('192.168.x.y',11211),
     'en'=>array('192.168.y.x',11211)
     );
$mysql    = array(
     'master'=>array('x','root','','test'),
     'slave_1'=>array('y','root','','test'),
     'slave_2'=>array('z','root','','test') //可以灵活添加多台从服务器
     );
?>




init.php


<?php
class Memcached
{
private $mem;
public $pflag=''; // memcached pconnect tag
    private function memConnect($serkey){     //建立memcached连接,这些都很容易理解
                require 'config.php';
                $server = $memcached;  //$memcached是cn,en的数组
                $this->mem = new Memcache;  
                $link = !$this->pflag ? 'connect' : 'pconnect' ;
                $this->mem->$link($server[$serkey][0],$server[$serkey][1]) or $this->errordie('memcached connect error');
      }

    public function set($ser_key,$values,$flag='',$expire=''){  //设置获取数据
               $this->memConnect($this->tag($ser_key));
                if($this->mem->set($ser_key,$values,$flag,$expire)) return true;
                else return false;
      }

    public function get($ser_key){
               $this->memConnect($this->tag($ser_key));
               if($var=$this->mem->get($ser_key)) return $var;
               else return false;
      }
    private function tag($ser_key){
               $tag=explode('_',$ser_key);
               return $tag[0];
      }
    private function errordie($errmsg){
                die($errmsg);
      }
/*  这段函数是我用来在类中做调试用的。
    public function show($messages){
               echo $messages;
    }
*/
}
?>





mysqli.php

<?php
class Mysql
{
/* 这段函数是我用来调试的
public function show($messages){
               var_dump($messages);
    }
*/
private   $mysqlmaster;
private   $myssqlslave;

public function __construct(){   //构造函数
         require 'config.php';
         $msg = $mysql;          //$mysql是master,slave_1,slave_2
         $this->mysqlmaster = new mysqli($msg['master'][0],$msg['master'][1],$msg['master'][2],$msg['master'][3]); //master mysql
         $this->mysqlslave = $this->autotranscat($msg); // slave mysql

         if(mysqli_connect_errno()){
                     printf("Connect failed: %s\n",mysqli_connect_error());
                      exit();
          }
        if(!$this->mysqlmaster->set_charset("latin1") && !$this->mysqlslave->set_charset("latin1")){
                    exit("set charset error");
          }
}
private function autotranscat($mysql){        //这段函数的作用是获取从服务器序列号,如1,2
        session_start();                      //启动会话
       $_SESSION['SID']!=0 || $_SESSION['SID']=0;  //如果不为0,就不做赋值操作(那第一次访问的话,肯定会做赋值操作的)
        if($_SESSION['SID'] >=count($mysql)-1) $_SESSION['SID'] = 1;  //上面那个动作是为这个动作准备的
        else $_SESSION['SID']++;
        $key = 'slave_'.$_SESSION['SID'];
        return new mysqli($mysql[$key][0],$mysql[$key][1],$mysql[$key][2],$mysql[$key][3]);
}

public function mquery($sql){ //mysql主 insert update
if(!$this->mysqlmaster->query($sql)){
   return false;
}
}

public function squery($sql){   //mysql从 查询
if($result=$this->mysqlslave->query($sql)){
   return $result;
}else{
   return false;
};
}
public function fetArray($sql){  //mysql从 查询 入口
if($result=$this->squery($sql)){
   while($row=$result->fetch_array(MYSQLI_ASSOC)){
    $resultraa[] = $row;
   };
   return $resultraa;
}
}
}
?>

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2627/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]