Zend Framework中如何实现MySQL的读写分离

<?php

/**
* Model
*
* @author li
* @version
*/

require_once 'Zend/Db/Table/Abstract.php';

abstract class Model extends Zend_Db_Table_Abstract {
private $_master;
private $_slave;
private $_dbconf;
protected function _setup(){
if (!$this->_slave instanceof Zend_Db_Adapter_Abstract){
$this->setDblink(0);
}
$frontendOptions = array(
'lifeTime' => 86400, // 两小时的缓存生命期
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => _CACHEPATH_.'/models',
);
$cache = Zend_Cache::factory('Core','File', $frontendOptions, $backendOptions);
$this->_metadataCache=$cache;
parent::_setup();
}
public function db_getprofiler(){
$db=$this->getDefaultAdapter();
$profiler = $db->getProfiler();
$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT |
Zend_Db_Profiler::QUERY
);
$result = $profiler->getQueryProfiles();
$totalTime = $profiler->getTotalElapsedSecs();
$queryCount = $profiler->getTotalNumQueries();
$longestTime = 0;
$longestQuery = null;
foreach ($result as $query) {
if ($query->getElapsedSecs() > $longestTime) {
$longestTime = $query->getElapsedSecs();
$longestQuery = $query->getQuery();
}
}
echo '共执行 ' . $queryCount . ' 次查询 在 ' . $totalTime .
' 秒' . "<br/>";
echo '平均查询时间: ' . $totalTime / $queryCount .
' 秒<br/>';
echo '每秒执行查询数: ' . $queryCount / $totalTime . "<br/>";
echo '最长查询时间: ' . $longestTime . "<br/>";
echo "最长查询语句: \n" . $longestQuery . "<br/>";
}
public function insert($data){
if (!$this->_master instanceof Zend_Db_Adapter_Abstract){
$this->setDblink(1);
}
$this->_master->insert($this->_name,$data);
}
public function update($data,$where){
if (!$this->_master instanceof Zend_Db_Adapter_Abstract){
$this->setDblink(true);
}
$this->_master->update($this->_name,$data,$where);
}
/**
* 连接数据库
* @param int $ismaster 1 连主库 0 连辅库
*/
private function setDblink($ismaster){
switch(_DOMAINPRI_){
default:
$strInclude = _CONFPATH_.'/db/db.config.php';
}
if(!is_array($this->_dbconf)){
$this->_dbconf = include_once($strInclude);
}
if($ismaster){
$param = $this->_dbconf['master'];
$this->_master = Zend_Db::factory($param['adapter'],$param['params']);
}else{
$rand = array_rand($this->_dbconf['slave']);
$param=$this->_dbconf['slave'][$rand];
$this->_slave = Zend_Db::factory($param['adapter'],$param['params']);
$this->setDefaultAdapter($this->_slave);
}
}
/**
* 设置主库为当默认Adapter
*/
public function setMasterasDefault(){
if (!$this->_master instanceof Zend_Db_Adapter_Abstract){
$this->setDblink(1);
}
$this->setDefaultAdapter($this->_master);
}
/**
* 设置辅库为当默认Adapter
*/
public function setSlaveDefault(){
if (!$this->_master instanceof Zend_Db_Adapter_Abstract){
$this->setDblink(0);
}
$this->setDefaultAdapter($this->_slave);
}
}
?>

转载于:https://www.cnblogs.com/lfq618/archive/2012/07/24/2606585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值