資料庫讀、寫分離,也是一種調整資料庫系統效能的方法。如果你的專案使用CakePHP,ㄚ凱(Darkhero)的這篇文章,可以讓你輕鬆透過設定,達到讀、寫分離的效果。
完整的介紹請到ㄚ凱隨手記
第一步:設定database.php
<?php
class DATABASE_CONFIG {
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => '192.160.1.110',
'port' => '',
'login' => 'root',
'password' => '1234',
'database' => 'cakephp',
'schema' => '',
'prefix' => '',
'encoding' => 'UTF8'
);
public $master = array(
'driver' => 'mysql',
'persistent' => false,
'host' => '192.168.1.100',
'port' => '',
'login' => 'root',
'password' => '',
'database' => 'cakephp',
'schema' => '',
'prefix' => '',
'encoding' => 'UTF8'
);
}
?>
第二步,修改app_model.php
<?php
class AppModel extends Model {
function beforeSave() {
$this->useDbConfig = 'master';
}
function afterSave() {
$this->useDbConfig = 'default';
}
function beforeDelete() {
$this->useDbConfig = 'master';
}
function afterDelete() {
$this->useDbConfig = 'default';
}
}
?>