资料库读、写分离,也是一种调整资料库系统效能的方法。如果你的专案使用CakePHP,ㄚ凯(Darkhero)的这篇文章,可以让你轻鬆透过设定,达到读、写分离的效果。
完整的介绍请到ㄚ凯随手记
第一步:设定database.php
<?phpclass 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
<?phpclass AppModel extends Model { function beforeSave() { $this->useDbConfig = 'master'; } function afterSave() { $this->useDbConfig = 'default'; } function beforeDelete() { $this->useDbConfig = 'master'; } function afterDelete() { $this->useDbConfig = 'default'; }}?>