整理php操作memcache缓存基础方法

php操作memcache缓存方法分享


使用memcache的前提是需要在服务端先配置好memcahche的环境!确认memcahce可以正常连接之后就可以在程序使用了!


[php]  view plain copy
  1. <?php  
  2. /** 
  3.  * Memcache缓存操作 
  4.  * @update http://www.lai18.com 
  5.  */  
  6. class MCache extends Object implements CacheFace  
  7. {  
  8.   private $mem = null; //Mem对象  
  9.      
  10.   private $sId = 1;  //servier服务ID  
  11.      
  12.   /** 
  13.    * 初始化Memcache 
  14.    * 
  15.    * @return Object 
  16.    */  
  17.   public function __construct()  
  18.   {  
  19.     if ( !class_exists('Memcache') )  
  20.     {  
  21.       throw new QException('PHP extension does not exist: Memcache');  
  22.     }  
  23.     $this->mem = new Memcache();  
  24.   }  
  25.      
  26.   /** 
  27.    * 链接memcahce服务 
  28.    * 
  29.    * @access private 
  30.    * @param  string $key 关键字 
  31.    * @param  string $value 缓存内容 
  32.    * @return array 
  33.    * @update http://www.lai18.com 
  34.    */  
  35.   private function connect( $sid )  
  36.   {  
  37.     $file = $this->CacheFile();  
  38.     require $file;  
  39.     if(! isset($cache) )  
  40.     {  
  41.       throw new QException('缓存配置文件不存在'.$file);  
  42.     }  
  43.     $server = $cache[$this->cacheId];  
  44.     $sid  = isset($sid) == 0 ? $this->sId : $sid;//memcache服务选择  
  45.     if ( ! $server[$sid])  
  46.     {  
  47.       throw new QException('当前操作的缓存服务器配置文件不存在');  
  48.     }  
  49.     $host = $server[$sid]['host'];  
  50.     $port = $server[$sid]['port'];  
  51.     try {  
  52.       $this->mem->connect( $host , $port );  
  53.     } catch (Exception $e) {  
  54.       exit('memecache连接失败,错误信息:'$e->getMessage());  
  55.     }  
  56.   }  
  57.      
  58.   /** 
  59.    * 写入缓存 
  60.    * 
  61.    * @access private 
  62.    * @param  string $key 关键字 
  63.    * @param  string $value 缓存内容 
  64.    * @return array 
  65.    */  
  66.   public function set( $key , $value , $sid , $expire = 0)  
  67.   {  
  68.     $data = $this->get($key , $sid); //如果已经存在key值  
  69.     if$data )   
  70.     {  
  71.       return $this->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire);  
  72.     } else {  
  73.       return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire);  
  74.     }  
  75.   }  
  76.      
  77.   /** 
  78.    * 读取缓存 
  79.    * 
  80.    * @access private 
  81.    * @param  string $key 关键字 
  82.    * @param  int   $sid 选择第几台memcache服务器 
  83.    * @return array 
  84.    */  
  85.   public function get( $key , $sid)  
  86.   {  
  87.     $this->connect( $sid );  
  88.     return $this->mem->get($key);  
  89.   }  
  90.      
  91.   /** 
  92.    * 清洗(删除)已经存储的所有的元素 
  93.    * 
  94.    * @access private 
  95.    * @return array 
  96.    */  
  97.   public function flush()  
  98.   {  
  99.     $this->connect();  
  100.     return $this->mem->flush();  
  101.   }  
  102.   /** 
  103.    * 删除缓存 
  104.    * 
  105.    * @access private 
  106.    * @param  string $key 关键字 
  107.    * @param  int   $sid 选择第几台memcache服务器 
  108.    * @return array 
  109.    */  
  110.   public function remove( $key , $sid)  
  111.   {  
  112.     $this->connect();  
  113.     return $this->mem->delete($key);  
  114.   }  
  115.      
  116.   /** 
  117.    * 析构函数 
  118.    * 最后关闭memcache 
  119.    */  
  120.   public function __destruct()  
  121.   {  
  122.     /*if(! $this->mem) 
  123.     { 
  124.       $this->mem->close(); 
  125.     }*/  
  126.   }  
  127. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值