php中memcache缓存类的代码分享

发布时间:2020-09-27编辑:脚本学堂
本文介绍下,php写的二个memcache缓存类的代码,有需要的朋友作个参考。

代码1,操作memcache服务器。

<?php   
class mem{  
    private static $conn;  
    public function mem($host='127.0.0.1',$port=11211)  
    {  
        $conn = new Memcache;  
        $conn->pconnect($host, $port);  
    }  
      
    public function get($key)  
    {  
        return $this->conn->get($key);  
    }  
      
    public function set($key,$value,$expire=259200)  
    {  
        return $this->conn->set($key,$value,0,$expire);  
    }  
      
    public function del($key)  
    {  
        return $this->conn->delete($key);  
    }  
      
    public function clearAll()  
    {  
        return $this->conn->flush();  
    }  
}  
?>

代码2,从数据库取数据,如果取不到,则自动加载设定的表到内存,假设,每个表的主键字段是id。

<?php
//memcache缓存类
//by www.jb200.com   
require_once('mem.php');  
require_once('common.fun.php');  
class cache{  
    private static $mem;  
    private static $fixedTables=array(  
                                    "enemy",  
                                    "equip",  
                                    "soldier_skill"  
                                    );  
      
    private static $fixedTablesHasCol2Search=array(  
                                    "soldier"=>array("soldier_id")                                     
                                    );  
      
    private $db;  
    public function cache($host='127.0.0.1',$port=11211,&$db)  
    {  
        self::$mem = new mem($host,$port);  
        $this->db=$db;  
    }  
      
    public function get($tb,$pkv)  
    {  
        $return = self::$mem->get($tb.'_'.$pkv);  
        if(!$return)  
        {  
            $this->loadAll();  
            $return = self::$mem->get($tb.'_'.$pkv);  
        }  
        if(!$return) common::dolog('cache class get "'.$tb.'_'.$pkv.'" failed');  
        return $return;  
    }  
      
    public function getByCol($tb,$col,$kv)  
    {  
        $return = self::$mem->get($tb.'_'.$col.'_'.$kv);  
        if(!$return)  
        {  
            $this->loadAll();  
            $return = self::$mem->get($tb.'_'.$col.'_'.$kv);  
        }  
        if(!$return) common::dolog('cache class getByCol "'.$tb.'_'.$col.'_'.$kv.'" failed');  
        return $return;  
    }  
      
    public function loadAll()  
    {  
        foreach(self::$fixedTables as $tb)  
        {  
            $rows=$this->db->getRows('select '.chr(42).' from '.$tb);  
            $seachCol=array();  
            $searhColData=array();  
            if(isset(self::$fixedTablesHasCol2Search[$tb]))  
            {  
                $seachCol=self::$fixedTablesHasCol2Search[$tb];  
            }  
            foreach($rows as $row)  
            {  
                self::$mem->set($tb.'_'.$row['id'],$row);  
                if(!empty($seachCol))  
                {  
                    foreach($seachCol as $col)  
                    {  
                        $searhColData[$tb.'_'.$col.'_'.$row[$col]][]=$row;  
                    }  
                }  
            }  
            foreach($searhColData as $k=>$v)       
            {  
                self::$mem->set($k,$v);  
            }  
        }  
    }  
      
    public function set($key,$value,$expire=259200)  
    {  
        return self::$mem->set($key,$value,0,$expire);  
    }  
      
    public function del($key)  
    {  
        return self::$mem->del($key);  
    }  
}  
?>