yii2.0主从数据库实现源码分析-----(二)
来源:互联网 发布:mac顶部菜单栏 编辑:程序博客网 时间:2024/06/05 22:32
这篇主要是介绍当主或者从服务器挂掉之后,yii的处理
connection.php
$cache = is_string($this->serverStatusCache) ? Yii::$app->get($this->serverStatusCache, false) : $this->serverStatusCache;<span style="color:#ff0000;">//获取配置文件中的cache</span> shuffle($pool); foreach ($pool as $config) {<span style="color:#ff0000;">//遍历数据库配置项,从这里也看出来了,再配置数据库的时候,一般masters/slaves是个二维数组,而config是一维的用来共享</span> $config = array_merge($sharedConfig, $config); if (empty($config['dsn'])) { throw new InvalidConfigException('The "dsn" option must be specified.'); } $key = [__METHOD__, $config['dsn']]; if ($cache instanceof Cache && $cache->get($key)) {<span style="color:#ff0000;">//会先判断cache里面是否有$key,有$key说明这个库连接失败过,而且还没过那个时间</span> // should not try this dead server now continue; } /* @var $db Connection */ $db = Yii::createObject($config); try { $db->open(); return $db; } catch (\Exception $e) { Yii::warning("Connection ({$config['dsn']}) failed: " . $e->getMessage(), __METHOD__); if ($cache instanceof Cache) { // mark this server as dead and only retry it after the specified interval $cache->set($key, 1, $this->serverRetryInterval);<span style="color:#ff0000;">//初始化PDO失败,写入缓存记录,有效期是$this->serverRetryInterval</span> } } }
/** * @var Cache|string the cache object or the ID of the cache application component that is used to store * the health status of the DB servers specified in [[masters]] and [[slaves]]. * This is used only when read/write splitting is enabled or [[masters]] is not empty. */ public $serverStatusCache = 'cache';<span style="color:#ff0000;">//缓存,存放标志位</span> /** * @var integer the retry interval in seconds for dead servers listed in [[masters]] and [[slaves]]. * This is used together with [[serverStatusCache]]. */ public $serverRetryInterval = 600;<span style="color:#ff0000;">//时间,过多久之后才会重试之前宕掉的数据库服务器</span>
在main.php中
<?phpreturn [ 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', 'components' => [ 'cache' => [ 'class' => 'yii\caching\FileCache',<span style="color:#ff0000;">//默认的缓存配置</span> ], ],];
从上面的源码中可以看出来,每次请求到来,yii初始化PDO的时候,对于上次宕掉的数据库服务器,采用了标志位避免了轮询,而是每隔一定的时间,来初始化
清除缓存的方法
$cache=Yii::$app->get("cache");$cache->flush();
1 0
- yii2.0主从数据库实现源码分析-----(二)
- yii2.0主从数据库实现源码分析
- 使用Yii2实现读写分离(MySQL主从数据库)
- Yii2.0-advanced-10—数据库操作6(读写分离-数据库主从配置)
- tokyo tyrant源码分析-主从复制实现
- YII2框架学习 基础篇(二) YII2数据库操作
- YII2 请求源码分析
- mysql数据库主从配置详解以及主从实现原理分析
- yii2.0源码实现csrf验证
- Yii2的主从数据库如何设置?
- derby数据库源码分析(二)--页面分析
- Laravel 5 配置数据库主从读写分离和源码分析
- yii2 分页类源码分析
- yii2底层源码分析1
- Redis源码分析(十九)--- replication主从数据复制的实现
- [ActiveForm] -- Yii2.0源码分析之——创建表单(ActiveForm)
- yii2.0验证规则源码分析&php正则使用
- [源码学习] -- yii2源码学习笔记(二)
- lintcode-将数组重新排序以构造最小值
- spring 下载
- Java笔记整理:初始化与清理
- 文章标题
- 基于Vue.js的表格分页组件
- yii2.0主从数据库实现源码分析-----(二)
- ndk 少写return 0;引发的血案
- 天气
- CentOS系统中Maven编译出错 dependencies could not be resolved
- pmtest5源码解析——特权级变换与堆栈切换实例详解
- Java NIO系列教程(二) Channel
- hive添加完hive.exec.stagingdir参数,有的SQL报FileNotFoundException错
- 校准raspberry ip 时间的方法
- 更改pip源至国内镜像,显著提升下载速度