国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > php教程 > php手冊 > 正文

php操作redis緩存方法分享,phpredis緩存分享

php中文網(wǎng)
發(fā)布: 2016-06-13 09:02:44
原創(chuàng)
1519人瀏覽過

php操作redis緩存方法分享,phpredis緩存分享

php redis緩存操作

<&#63;php
/**
 * Redis緩存操作
 * @author hxm
 * @version 1.0
 * @since 2015.05.04
 */
class RCache extends Object implements CacheFace 
{
  private $redis = null; //redis對象
   
  private $sId  = 1;  //servier服務(wù)ID
   
  private $con  = null;//鏈接資源
   
  /**
   * 初始化Redis
   *
   * @return Object
   */
  public function __construct()
  {
    if ( !class_exists('Redis') )
    {
      throw new QException('PHP extension does not exist: Redis');
    }
    $this->redis = new Redis();
  }
   
  /**
   * 鏈接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 &#63; $this->sId : $sid;//memcache服務(wù)選擇
    if ( ! $server[$sid])
    {
      throw new QException('當(dāng)前操作的緩存服務(wù)器配置文件不存在');
    }
    $host = $server[$sid]['host'];
    $port = $server[$sid]['port'];
    try {
      $this->redis->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->redis->getset( $key , $value);
    } else {
      return $this->redis->set( $key , $value);
    }
  }
   
  /**
   * 讀取緩存
   *
   * @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->redis->get($key);
  }
   
  /**
   * 清洗(刪除)已經(jīng)存儲(chǔ)的所有的元素
   *
   * @access private
   * @return array
   */
  public function flush()
  {
    $this->connect();
    return $this->redis->flushall();
  }
  /**
   * 刪除緩存
   *
   * @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->redis->del($key);
  }
   
  /**
   * 析構(gòu)函數(shù)
   * 最后關(guān)閉memcache
   */
  public function __destruct()
  {
    if($this->redis)
    {
      $this->redis->close();
    }
  }
}
登錄后復(fù)制

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

PHP速學(xué)教程(入門到精通)
PHP速學(xué)教程(入門到精通)

PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!

下載
相關(guān)標(biāo)簽:
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費(fèi)商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)