資料庫讀寫分離
當系統越來越大的時候...
資料庫的負擔也會越來越大..這時候通常都會靠讀寫分離來讓系統更穩定. 讓資料庫更有效率...
在 CakePHP 可以靠下面的設定輕鬆達到...
database.php 的設定中..
[code]<?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'
);
}
?>[/code]
先在 database 加入主要負責寫入的 master 主機...而一般負責讀取的主機就放在 default .
然後在 app_model.php 加入下面的處理...
[code]<?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';
}
}
?>
[/code]
這樣在實際執行的時候就會在寫入資料的時候自動切換到 $master 去進行寫入的動作了..







迴響
$slave[]=....; blah blah
然後 rand 去隨機跑,
最近要打算寫一個像google calendar 的東西
打算 clone 一個出來,javascript framework
是用 mootools,
又有點想用 php + ajax 的 lib
因為不論是 yui ,mootools,prototype 都要用很
多的 css 和 js,因為我是用自己的 MVC 方法,反而寫 php 的時間少,
寫 css 和 js 的花的時間
多.....orz
---
我要把這裡變的跟朱學恆的部落格一樣
留言比文章多.....flee
不然在會是false ..
if ($this->exists() && $this->beforeDelete($cascade)) {