- N*** 이벤트 프로젝트시 트래픽 부하 방지를 위해 Insert용 DB와 Select용 DB를 나누었다.
1. 새로운 DB 배열 생성
- www/application/config/database.php 에 DB 정보가 저장되어져음.
- $db[‘default’] 와 $db[‘slave’] 생성.
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'default_db',
'password' => 'default_db_password',
'database' => 'default_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['slave'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'slave_db',
'password' => 'slave_db_password',
'database' => 'slave_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
2. Model 등록하기
- www/application/third_party/MX/Controller.php 에서 model 등록
- $db[‘default’] 와 $db[‘slave’] 생성.
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
require dirname(__FILE__).'/Base.php';
class MX_Controller
{
public $autoload = array();
public $language = 'kr';
public $folder;
public $is_mobile = 0;
public $theme_path;
public $nike_theme_path;
public $module;
public $site_meta=array(); //사이트전체 메타정보
public $meta=array(); //메뉴 메타정보
public $mid;
public function __construct()
{
$class = str_replace(CI::$APP->config->item('controller_suffix'), '', get_class($this));
log_message('debug', $class." MX_Controller Initialized");
Modules::$registry[strtolower($class)] = $this;
/* copy a loader instance and initialize */
$this->load = clone load_class('Loader');
$this->load->initialize($this);
/* autoload module items */
$this->load->_autoloader($this->autoload);
$this->load->helper(array('form', 'url','file','text','login_helper'));
$this->load->model(array('common_model','common2_model')); // 여기에 추가하기.
$this->load->library(array('email','user_agent','form_validation','alert','session','encryption'));
if(file_exists(FCPATH.'/common/json/config.basic.json'))
{
$json = read_file(FCPATH.'/common/json/config.basic.json');
$this->site_meta = json_decode($json,true);
}
$uri_array = segment_explode($this->uri->uri_string());
if(is_array($uri_array)){ //언어별 분기
switch($uri_array[0]){
case('en'):
$this->language = $uri_array[0];
break;
case('jp'):
$this->language = $uri_array[0];
break;
case('cn'):
$this->language = $uri_array[0];
break;
default:
break;
}
}
//force_https();
//테마경로
if(DEVICE_TYPE == 'M'){
$this->is_mobile = 1;
$this->folder .= '/m';
}
$this->theme_path = '/theme/'.$this->config->item('theme').'/'.$this->language.$this->folder;
}
public function __get($class)
{
return CI::$APP->$class;
}
}
3. Common_model 추가로 생성하기
- www/application/models/Common_model2.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class common_model2 extends CI_Model {
//모델 생성자 호출
function __construct(){
parent::__construct();
$this->load->helper('file');
$this->slave = $this->load->database('slave', TRUE);
$this->load->dbforge($this->slave,true); // 여기에서 $this->slave 로딩.
}
function _total($config){
if($config['where']) $this->slave->where($config['where'],null,false);
if(array_key_exists('where_or',$config)) $this->slave->or_where($config['where_or'],null,false);
$count = $this->slave->count_all_results($config['table']);
//echo $this->slave->last_query();
return $count;
}
function _list($config){
$page = (array_key_exists('page',$config)) ? $config['page'] : 1 ;
$listnum = (array_key_exists('listnum',$config)) ? $config['listnum'] : 10 ;
$offset = $listnum *($page-1);
//if(!array_key_exists('orderby',$config)) $config['orderby'] = ' seq DESC';
if(array_key_exists('orderby',$config)) $this->slave->order_by($config['orderby']);
if(array_key_exists('select',$config)) $this->slave->select($config['select']);
if(array_key_exists('where',$config)) $this->slave->where($config['where'],null,false);
if(array_key_exists('where_or',$config)) $this->slave->or_where($config['where_or'],null,false);
if(array_key_exists('join',$config))
{
foreach($config['join'] as $k => $v)
{
$this->slave->join($v[0], $v[1], $v[1]);
}
}
$this->slave->from($config['table']);
$this->slave->limit($listnum,$offset);
$query = $this->slave->get();
//echo $this->slave->last_query();
return $query;
}
function _ajaxList($config){
$page = (array_key_exists('page',$config)) ? $config['page'] : 1 ;
$listnum = (array_key_exists('listnum',$config)) ? $config['listnum'] : 10 ;
$offset = 0;
if(array_key_exists('orderby',$config)) $this->slave->order_by($config['orderby']);
if(array_key_exists('select',$config)) $this->slave->select($config['select']);
if(array_key_exists('where',$config)) $this->slave->where($config['where'],null,false);
if(array_key_exists('where_or',$config)) $this->slave->or_where($config['where_or'],null,false);
if(array_key_exists('join',$config))
{
foreach($config['join'] as $k => $v)
{
$this->slave->join($v[0], $v[1], $v[1]);
}
}
$this->slave->from($config['table']);
$this->slave->limit($listnum,$offset);
$query = $this->slave->get();
//echo $this->slave->last_query();
return $query;
}
function _select($config){
$data = null;
if(array_key_exists('where',$config)){
if(array_key_exists('select',$config)){
$this->slave->select($config['select']);
}else{
$this->slave->select("*");
}
if(array_key_exists('where',$config)){
$this->slave->where($config['where'],null,false);
}
$query = $this->slave->get($config['table']);
//echo $this->slave->last_query();
if($query->num_rows() > 0){
$row = $query->row_array();
$data = $row;
}else{
$fields = $this->slave->list_fields($config['table']);
foreach ($fields as $field){
$data[$field] = null;
}
}
}
if(!$data){
$fields = $this->slave->list_fields($config['table']);
foreach ($fields as $field){
$data[$field] = null;
}
}
return $data;
}
function _proc($config){
if(array_key_exists('where_in',$config))
{
$this->slave->where_in($config['where_in'],$config['where'],false);
}
elseif(array_key_exists('where',$config))
{
$this->slave->where($config['where'],null,false);
}
switch($config['dbjob']){
case('i'):
$this->slave->insert($config['table'], $config['data']);
break;
case('u'):
$this->slave->update($config['table'], $config['data']);
break;
case('d'):
$this->slave->delete($config['table']);
break;
}
//echo $this->slave->last_query();
}
}
?>
4. Controller 에서 Common_model2 사용
$query = $this->common_model2->_list($config);