memeache 類封裝

<?php
 /**
  * 此類爲單例模式,取的實例的方法 :$cache=Mcache:getInstance();
  * 
  *
  */
  class Mcache{
  private static $_instance;
  private static $_connect_type='';
  private $_memcache;

  /**
  *私有化構造函數 ,禁止使用關鍵字new來實例化Mcache類
  */
  private function __construct(){
   if(!class_exists('Memcache')){
   throw new Exception('Class Memcache not exists ');
   }
    $func=self::$_connect_type;
    $this->_memcache=new Memcache();
    $this->_memcache->$func(MEMCACHE_SERVER,MEMCACHE_PORT);
  }
/**
 *克隆私有化,禁止克隆實例
  */
    function __clone(){}
    /**
     *類的入口,通過此類的靜態方法對類進行實例化
     */
     public static function getInstance($type='connect'){

      self::$_connect_type=($type=='connect')?$type:'pconnect';
      if (!self::$_instance instanceof self){
      self::$_instance=new self();
      }
      return self::$_instance;
     }

     /**
      *把數據添加到緩存
      *param string $key 緩存的key
      *param string |arrary|int ....$value 緩存的數據
      *param int $expire_time 緩存時間
      */
     public function set($key,$value,$expire_time=0){
      if($expire_time>0){
      $this->_memcache->set($key,$value,0,$expire_time);
      }else{
          $this->_memcache->set($key,$value);
      }
     }

     /**
      * 從緩存讀取數據
      * @package string|array|int ....$key
      */
    public  function get($key){
     return $this->_memcache->get($key);
    }
    /**
     * 從緩存中刪除數據
     * @param sting|array|int...$key
     */
    public function del($key){

     return $this->_memcache->delete($key);
    }

  }
  ?>
發佈了16 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章