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

php操作memcache緩存方法分享

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

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

<?php
/**
 * Memcache緩存操作
 * @author hxm
 * @version 1.0
 * @since 2015.05.04
 */
class MCache extends Object implements CacheFace
{
  private $mem = null; //Mem對象
   
  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連接失敗,錯誤信息:'. $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 選擇第幾臺memcache服務(wù)器
   * @return array
   */
  public function get( $key , $sid)
  {
    $this->connect( $sid );
    return $this->mem->get($key);
  }
   
  /**
   * 清洗(刪除)已經(jīng)存儲的所有的元素
   *
   * @access private
   * @return array
   */
  public function flush()
  {
    $this->connect();
    return $this->mem->flush();
  }
  /**
   * 刪除緩存
   *
   * @access private
   * @param  string $key 關(guān)鍵字
   * @param  int   $sid 選擇第幾臺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();
    }*/
  }
}

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

相關(guān)文章

最新評論

乌鲁木齐县| 固原市| 双鸭山市| 陆川县| 南郑县| 隆林| 文昌市| 永善县| 佛教| 阳山县| 吉安市| 金堂县| 洛隆县| 沙雅县| 简阳市| 石首市| 邯郸市| 广西| 永寿县| 衡阳市| 于都县| 阿拉善右旗| 永登县| 寿宁县| 阳原县| 中宁县| 长宁区| 广宁县| 曲沃县| 龙口市| 综艺| 天等县| 旅游| 洪泽县| 安仁县| 乌什县| 前郭尔| 建德市| 田阳县| 班玛县| 石首市|