最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php操作memcache緩存方法分享

 更新時(shí)間:2015年06月03日 12:20:46   投稿:hebedich  
一般來(lái)說,如果并發(fā)量不大的情況,使不使用緩存技術(shù)并沒有什么影響,但如果高并發(fā)的情況,使用緩存技術(shù)就顯得很重要了,可以很好的減輕數(shù)據(jù)庫(kù)和服務(wù)器的壓力,當(dāng)然解決高并發(fā)的技術(shù)有很多,這里只是以緩存的角度來(lái)說明使用memcache的便捷性和方便性,

使用memcache的前提是需要在服務(wù)端先配置好memcahche的環(huán)境!確認(rèn)memcahce可以正常連接之后就可以在程序使用了!

<?php
/**
 * Memcache緩存操作
 * @author hxm
 * @version 1.0
 * @since 2015.05.04
 */
class MCache extends Object implements CacheFace
{
  private $mem = null; //Mem對(duì)象
   
  private $sId = 1;  //servier服務(wù)ID
   
  /**
   * 初始化Memcache
   *
   * @return Object
   */
  public function __construct()
  {
    if ( !class_exists('Memcache') )
    {
      throw new QException('PHP extension does not exist: Memcache');
    }
    $this->mem = new Memcache();
  }
   
  /**
   * 鏈接memcahce服務(wù)
   *
   * @access private
   * @param  string $key 關(guān)鍵字
   * @param  string $value 緩存內(nèi)容
   * @return array
   */
  private function connect( $sid )
  {
    $file = $this->CacheFile();
    require $file;
    if(! isset($cache) )
    {
      throw new QException('緩存配置文件不存在'.$file);
    }
    $server = $cache[$this->cacheId];
    $sid  = isset($sid) == 0 ? $this->sId : $sid;//memcache服務(wù)選擇
    if ( ! $server[$sid])
    {
      throw new QException('當(dāng)前操作的緩存服務(wù)器配置文件不存在');
    }
    $host = $server[$sid]['host'];
    $port = $server[$sid]['port'];
    try {
      $this->mem->connect( $host , $port );
    } catch (Exception $e) {
      exit('memecache連接失敗,錯(cuò)誤信息:'. $e->getMessage());
    }
  }
   
  /**
   * 寫入緩存
   *
   * @access private
   * @param  string $key 關(guān)鍵字
   * @param  string $value 緩存內(nèi)容
   * @return array
   */
  public function set( $key , $value , $sid , $expire = 0)
  {
    $data = $this->get($key , $sid); //如果已經(jīng)存在key值
    if( $data ) 
    {
      return $this->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire);
    } else {
      return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire);
    }
  }
   
  /**
   * 讀取緩存
   *
   * @access private
   * @param  string $key 關(guān)鍵字
   * @param  int   $sid 選擇第幾臺(tái)memcache服務(wù)器
   * @return array
   */
  public function get( $key , $sid)
  {
    $this->connect( $sid );
    return $this->mem->get($key);
  }
   
  /**
   * 清洗(刪除)已經(jīng)存儲(chǔ)的所有的元素
   *
   * @access private
   * @return array
   */
  public function flush()
  {
    $this->connect();
    return $this->mem->flush();
  }
  /**
   * 刪除緩存
   *
   * @access private
   * @param  string $key 關(guān)鍵字
   * @param  int   $sid 選擇第幾臺(tái)memcache服務(wù)器
   * @return array
   */
  public function remove( $key , $sid)
  {
    $this->connect();
    return $this->mem->delete($key);
  }
   
  /**
   * 析構(gòu)函數(shù)
   * 最后關(guān)閉memcache
   */
  public function __destruct()
  {
    /*if(! $this->mem)
    {
      $this->mem->close();
    }*/
  }
}

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論

嘉兴市| 萍乡市| 张家川| 南京市| 宁远县| 白玉县| 巩留县| 敦化市| 洛扎县| 深泽县| 安西县| 龙胜| 政和县| 高唐县| 金昌市| 延长县| 迁安市| 楚雄市| 清远市| 卓资县| 江华| 玛曲县| 万源市| 清流县| 互助| 马关县| 临颍县| 新泰市| 乌兰县| 德兴市| 罗平县| 嘉禾县| 阿拉善左旗| 花莲市| 剑川县| 石林| 都江堰市| 安乡县| 昭通市| 皋兰县| 工布江达县|