当前位置:首页 > PHP教程 > php高级应用 > 列表

php操作memcache缓存方法分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-27 15:02:02 浏览: 评论:0 

一般来说,如果并发量不大的情况,使不使用缓存技术并没有什么影响,但如果高并发的情况,使用缓存技术就显得很重要了,可以很好的减轻数据库和服务器的压力,当然解决高并发的技术有很多,这里只是以缓存的角度来说明使用memcache的便捷性和方便性,使用memcache的前提是需要在服务端先配置好memcahche的环境!确认memcahce可以正常连接之后就可以在程序使用了!

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

Tags: memcache

分享到: