zoukankan      html  css  js  c++  java
  • [轉]資料庫讀寫分離

    From : http://blog.darkhero.net/archives/288

    當系統越來越大的時候…
    資料庫的負擔也會越來越大..這時候通常都會靠讀寫分離來讓系統更穩定. 讓資料庫更有效率…

    在 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 去進行寫入的動作了..

  • 相关阅读:
    Uva 10719 Quotient Polynomial
    UVa 11044 Searching for Nessy
    Uva 10790 How Many Points of Intersection?
    Uva 550 Multiplying by Rotation
    Uva 10916 Factstone Benchmark
    Uva 10177 (2/3/4)D Sqr/Rects/Cubes/Boxes?
    Uva 591 Box of Bricks
    Uva 621 Secret Research
    Uva 10499 The Land of Justice
    Uva 10014 Simple calculations
  • 原文地址:https://www.cnblogs.com/Athrun/p/php_db_read_write.html
Copyright © 2011-2022 走看看