php數(shù)據(jù)庫(kù)連接配合memcache

字號(hào):


    采用memcache與數(shù)據(jù)庫(kù)連接查詢的方式進(jìn)行數(shù)據(jù)緩存目前是采用單個(gè)的memcache服務(wù)器,以后會(huì)添加多個(gè)的
    <?php
    /**
    * @author
    * @name data link class and memcache class
    * //使用memcache的查詢結(jié)果
    * 傳送sql語(yǔ)句,返回是查詢后的數(shù)組,數(shù)組有可能為空
    * $dataArrayName = $db->get_Date($sql);
    * 如果查詢的是單條數(shù)據(jù),則要進(jìn)行輸出時(shí)采用
    * $dataArrayName[0]['字段名']的格式
    */
    class Datelink
    {
    private $DateServer = "localhost";//mysql數(shù)據(jù)庫(kù)地址
    private $DateBase = "basename";//mysql中的數(shù)據(jù)庫(kù)
    private $DateUser = "username";//mysql數(shù)據(jù)庫(kù)連接帳號(hào)
    private $Datepwd = "userpwd";//mysql數(shù)據(jù)庫(kù)連接密碼
    private $dbLink;//連接對(duì)象
    private $result;//數(shù)據(jù)查詢結(jié)果
    private $insert_id;//定義插入序號(hào)
    private $affected_rows;//定義影響行數(shù)
    static $data_obj;
    private $mem_obj;
    #采用單例模式,實(shí)例化時(shí)進(jìn)行數(shù)據(jù)庫(kù)連接
    function __construct(){
    $this->dbLink=@mysql_connect($this->DateServer,$this->DateUser,$this->Datepwd)or die(mysql_error());
    if(!$this->dbLink)$this->dbhalt("exsiting error when connecting!");
    if(!@mysql_select_db($this->DateBase,$this->dbLink))$this->dbhalt("can't use this database,please check database!");
    mysql_query("set names utf-8",$this->dbLink);//如果是utf-8可以改為utf-8
    $this->mem_obj = Mem::__GetObj();
    }
    private function __clone(){}
    static function contect_data(){
    if(!self::$data_obj instanceof self){
    self::$data_obj = new self();
    }
    return self::$data_obj;
    }
    function __set($name,$value){//設(shè)置屬性
    $this->$name=$value;
    }
    function __get($name){//獲取屬性
    return $this->$name;
    }
    function dbhalt($errmsg){//錯(cuò)誤反饋
    die($errmsg);
    }
    function get_Date($sql){//僅針對(duì)select 查詢進(jìn)行緩存
    if(preg_match("/^select/i",$sql)){//如果是select這里增加memcache內(nèi)容的判斷
    if($this->mem_obj->cache_Obj){//進(jìn)行mem 查詢看是否存在緩存
    if($temp=$this->mem_obj->get($sql)){
    $this->result=$temp;
    return $temp;
    }else{
    $this->execute($sql);
    $rows = $this->num_rows();
    $temp = array();
    for ($i=0;$i<$rows;$i++) {
    $fields = mysql_num_fields($this->result);
    $row = mysql_fetch_array($this->result);
    for ($j=0;$j<$fields;$j++) {
    if ($i == 0) {
    $columns[$j] = mysql_field_name($this->result,$j);
    }
    $temp[$i][$columns[$j]] = $row[$j];
    }
    }
    //$temp = $this->fetch_array();
    $this->mem_obj->set($sql,$temp);
    return $temp;
    }
    }//如果不是select 或者沒(méi)有memcache
    }
    //如果以上沒(méi)有進(jìn)行 memcache的查詢,則進(jìn)行普通查詢并返回
    $this->execute($sql);
    $rows = $this->num_rows();
    $temp = array();
    for ($i=0;$i<$rows;$i++) {
    $fields = mysql_num_fields($this->result);
    $row = mysql_fetch_array($this->result);
    for ($j=0;$j<$fields;$j++) {
    if ($i == 0) {
    $columns[$j] = mysql_field_name($this->result,$j);
    }
    $temp[$i][$columns[$j]] = $row[$j];
    }
    }
    return $temp;
    }
    function fetch_array(){
    return mysql_fetch_array($this->result);
    }
    function execute($sql){//執(zhí)行sql
    $this->result = mysql_query($sql,$this->dbLink);
    }
    function num_rows(){//返回行數(shù),參數(shù)記錄集
    return mysql_num_rows($this->result);
    }
    function get_rows($sql){//返回行數(shù),參數(shù)為sql
    $this->execute($sql);
    return $this->num_rows($this->result);
    }
    function insert($sql){//插入sql-有自動(dòng)增長(zhǎng)序號(hào),返回新建序號(hào)
    $this->execute($sql);
    $this->insert_id = mysql_insert_id($this->dbLink);
    $this->free_result($this->result);
    return $this->insert_id;
    }
    function insert_($sql){//插入sql-沒(méi)有自動(dòng)增長(zhǎng)序號(hào),返回影響行數(shù)
    $this->execute($sql);
    $this->affected_rows = mysql_affected_rows($this->dbLink);
    $this->free_result($this->result);
    return $this->affected_rows;
    }
    function update($sql){//更新sql
    $this->execute($sql);
    $this->affected_rows=mysql_affected_rows($this->dbLink);
    $this->free_result($this->result);
    return $this->affected_rows;
    }
    function del($sql){//刪除sql
    $this->execute($sql);
    $this->affected_rows=mysql_affected_rows($this->dbLink);
    $this->free_result($this->result);
    return $this->affected_rows;
    }
    function free_result(){//釋放記錄集
    @mysql_free_result($this->result);
    }
    function close(){//關(guān)閉當(dāng)前數(shù)據(jù)庫(kù)
    @mysql_close($this->dbLink);
    }
    }//結(jié)束class的括號(hào)
    class Mem{//memcache設(shè)置
    private $server_ip="";
    private $server_port="11211";//默認(rèn)端口
    static $mem_Obj;
    public $cache_Obj;
    function __construct(){
    if($this->cache_Obj=@new Memcache){
    if(!@$this->cache_Obj->connect($this->server_ip,$this->server_port))$this->cache_Obj=false; }
    }
    private function __clone(){}
    static function __GetObj(){
    if(!self::$mem_Obj instanceof self)self::$mem_Obj = new self;
    return self::$mem_Obj;
    }
    public function set($sql,$tempSource){//設(shè)置cache
    return $this->cache_Obj->set(md5($sql),$tempSource);
    }
    public function add($sql,$tmpSource){//add添加cache,如果存在則刪除后添加
    if($this->get($sql))$this->del($sql);
    return $this->set($sql,$tmpSource);
    }
    public function get($sql){//獲取cache
    if($temp=$this->cache_Obj->get(md5($sql))){
    return $temp;
    }
    return false;
    }
    public function del($sql){//刪除某個(gè)數(shù)據(jù)
    return $this->cache_Obj->delete(md5($sql));
    }
    public function delAll(){//讓所有cache失效,并不清空,會(huì)有新數(shù)據(jù)取代
    return $this->cache_Obj->flush();
    }
    }
    $db=Datelink::contect_data(); //這里是調(diào)用數(shù)據(jù)庫(kù)連接
    // $db->mem_obj->delAll();//這里可以其清除全部緩存
    ?>
    Memcache與mysql配合查詢,進(jìn)行數(shù)據(jù)緩存