我想在CodeIgniter 3中把我的應用程式從一個資料庫改為兩個資料庫。我使用的是Aurora AWS,所以我需要將資料庫分成兩個實體,一個用于讀取,第二個用于寫入。
我想我都做到了,但是在分割成兩個實體之后,在相同的流量下,我的資料庫連接數增加的非常多。我認為這是因為在每一個模型中,我都加載了第二個資料庫($this->db2 = $this->load->資料庫('writer_instance', TRUE);)。
紅色區域是我分割成兩個實體的時候。 藍色區域是我沒有分割成兩個實體的時候。
我應該怎么做?
我應該怎樣做才能使連接數不增加? 是否有任何選項可以將寫作者實體作為讀者實體加載一次?
下面是我的代碼:
我在database.php中添加了$active_group = 'reader_instance';
$query_builder = TRUE。
$db['reader_instance'] = array(
'dsn' => '',
'hostname' => '*',
'username' => '*',
'password' => '*',
'database' => '*',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE。
'db_debug' => (ENVIRONMENT !== 'production') 。
'cache_on' => FALSE,
'cachedir' => ''。
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE。
'stricon' => FALSE。
'failover' => array(),
'save_queries' => TRUE。
'port' => 3306; TRUE.
);
$db['writer_instance'] = array(
'dsn' => '',
'hostname' => '*',
'username' => '*',
'password' => '*',
'database' => '*',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE。
'db_debug' => (ENVIRONMENT !== 'production') 。
'cache_on' => FALSE,
'cachedir' => ''。
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE。
'stricon' => FALSE。
'failover' => array(),
'save_queries' => TRUE。
'port' => 3306; TRUE.
);
接下來,在每個模型中,當我加載第二個資料庫時,添加了結構。
<?php if ( ! defined('BASEPATH'/span>) exit('No direct script access allowed') 。
class ActionModerator_model extends CI_Model{
function __construct() {
parent::__construct()。
//db - reader_instance。
$this->db2 = $this->load->資料庫('writer_instance', TRUE)。
}
public function add($object)
{
$this->db2->insert( 'parameter_action' , $object ) 。
}
public function get($id_group)
{
$this->db->select('id, name') 。
$this->db->where( 'id_group' , $id_group ) 。
$q = $this->db->get( 'user' ) 。
$q = $q-> result();
return $q;
}
uj5u.com熱心網友回復:
我自己做了這個。我創建了庫:
<?php
class Databaseloader {
public function __construct() {
$this->load()。
}
public function load() {
$CI = &get_instance();
$CI->db = $CI->load->database('reader_instance', TRUE) 。
$CI->db2 = $CI->load->資料庫('writer_instance', TRUE)。
}
接下來在autoload.php中用databaseloader替換變數$autoload['library'] 資料庫,并在每個模型中洗掉加載資料庫。
$autoload['library'] = array('databaseloader')。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328407.html
標籤:


