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

thinkphp下MySQL數(shù)據(jù)庫(kù)讀寫(xiě)分離代碼剖析

 更新時(shí)間:2017年04月18日 14:52:18   作者:堅(jiān)持_只為攀登  
本篇文章主要介紹了thinkphp下MySQL數(shù)據(jù)庫(kù)讀寫(xiě)分離代碼剖析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

當(dāng)采用原生態(tài)的sql語(yǔ)句進(jìn)行寫(xiě)入操作的時(shí)候,要用execute,讀操作要用query。

MySQL數(shù)據(jù)主從同步還是要靠MySQL的機(jī)制來(lái)實(shí)現(xiàn),所以這個(gè)時(shí)候MySQL主從同步的延遲問(wèn)題是需要優(yōu)化,延遲時(shí)間太長(zhǎng)不僅影響業(yè)務(wù),還影響用戶體驗(yàn)。

thinkphp核心類Thinkphp/library/Model.class.php 中,query 方法,調(diào)用Thinkphp/library/Think/Db/Driver/Mysql.class.php

  /**
   * SQL查詢
   * @access public
   * @param string $sql SQL
   * @param mixed $parse 是否需要解析SQL 
   * @return mixed
   */
  public function query($sql,$parse=false) {
    if(!is_bool($parse) && !is_array($parse)) {
      $parse = func_get_args();
      array_shift($parse);
    }
    $sql =  $this->parseSql($sql,$parse);
    return $this->db->query($sql);
  }
  

調(diào)用Thinkphp/library/Think/Db/Driver/Mysql.class.php

  /**
   * 執(zhí)行查詢 返回?cái)?shù)據(jù)集
   * @access public
   * @param string $str sql指令
   * @return mixed
   */
  public function query($str) {
    if(0===stripos($str, 'call')){ // 存儲(chǔ)過(guò)程查詢支持
      $this->close();
      $this->connected  =  false;
    }
    $this->initConnect(false);
    if ( !$this->_linkID ) return false;
    $this->queryStr = $str;
    //釋放前次的查詢結(jié)果
    if ( $this->queryID ) {  $this->free();  }
    N('db_query',1);
    // 記錄開(kāi)始執(zhí)行時(shí)間
    G('queryStartTime');
    $this->queryID = mysql_query($str, $this->_linkID);
    $this->debug();
    if ( false === $this->queryID ) {
      $this->error();
      return false;
    } else {
      $this->numRows = mysql_num_rows($this->queryID);
      return $this->getAll();
    }
  }

上面初始化數(shù)據(jù)庫(kù)鏈接時(shí),initConnect(false),調(diào)用Thinkphp/library/Think/Db/Db.class.php,注意false、true代碼實(shí)現(xiàn)。true表示直接調(diào)用主庫(kù),false表示調(diào)用讀寫(xiě)分離的讀庫(kù)。

  /**
   * 初始化數(shù)據(jù)庫(kù)連接
   * @access protected
   * @param boolean $master 主服務(wù)器
   * @return void
   */
  protected function initConnect($master=true) {
    if(1 == C('DB_DEPLOY_TYPE'))
      // 采用分布式數(shù)據(jù)庫(kù)
      $this->_linkID = $this->multiConnect($master);
    else
      // 默認(rèn)單數(shù)據(jù)庫(kù)
      if ( !$this->connected ) $this->_linkID = $this->connect();
  }

  /**
   * 連接分布式服務(wù)器
   * @access protected
   * @param boolean $master 主服務(wù)器
   * @return void
   */
  protected function multiConnect($master=false) {
    foreach ($this->config as $key=>$val){
      $_config[$key]   =  explode(',',$val);
    }    
    // 數(shù)據(jù)庫(kù)讀寫(xiě)是否分離
    if(C('DB_RW_SEPARATE')){
      // 主從式采用讀寫(xiě)分離
      if($master)
        // 主服務(wù)器寫(xiě)入
        $r =  floor(mt_rand(0,C('DB_MASTER_NUM')-1));
      else{
        if(is_numeric(C('DB_SLAVE_NO'))) {// 指定服務(wù)器讀
          $r = C('DB_SLAVE_NO');
        }else{
          // 讀操作連接從服務(wù)器
          $r = floor(mt_rand(C('DB_MASTER_NUM'),count($_config['hostname'])-1));  // 每次隨機(jī)連接的數(shù)據(jù)庫(kù)
        }
      }
    }else{
      // 讀寫(xiě)操作不區(qū)分服務(wù)器
      $r = floor(mt_rand(0,count($_config['hostname'])-1));  // 每次隨機(jī)連接的數(shù)據(jù)庫(kù)
    }
    $db_config = array(
      'username' => isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0],
      'password' => isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0],
      'hostname' => isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0],
      'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],
      'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],
      'dsn'    => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],
      'params'  => isset($_config['params'][$r])?$_config['params'][$r]:$_config['params'][0],
      'charset'  => isset($_config['charset'][$r])?$_config['charset'][$r]:$_config['charset'][0],      
    );
    return $this->connect($db_config,$r);
  }

query方法參數(shù)為false,其他刪除、更新、增加讀主庫(kù)。這一點(diǎn)可以結(jié)合Thinkphp/library/Model.class.php中的delete、save、add操作,參數(shù)為true。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

宝清县| 车致| 广宗县| 四会市| 宁河县| SHOW| 溧阳市| 宜丰县| 台中市| 平湖市| 贵州省| 攀枝花市| 大荔县| 晋城| 门源| 阿城市| 通许县| 海兴县| 天津市| 庐江县| 双城市| 华阴市| 定日县| 南宁市| 阳朔县| 阳曲县| 岢岚县| 渝北区| 襄城县| 清涧县| 青阳县| 西和县| 新郑市| 庄河市| 安塞县| 汪清县| 长沙市| 晋中市| 信阳市| 仪陇县| 彰化市|