改进分布式写入数据后及时读取的问题

This commit is contained in:
thinkphp
2018-04-23 14:45:51 +08:00
parent 37b2d1689d
commit 21872be5af
3 changed files with 53 additions and 12 deletions

View File

@@ -116,6 +116,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
protected static $initialized = [];
/**
* 是否从主库读取(主从分布式有效)
* @var bool
*/
protected static $readMaster = false;
/**
* 构造方法
* @access public
@@ -171,6 +177,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->initialize();
}
/**
* 是否从主库读取数据(主从分布有效)
* @access public
* @param bool $master 是否从主库读取
* @return void
*/
public function readMaster($master)
{
static::$readMaster = $master;
}
/**
* 创建模型的查询对象
* @access protected
@@ -194,6 +211,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$queryClass = $this->query ?: $con->getConfig('query');
$query = new $queryClass($con, $this);
if (static::$readMaster) {
$query->master(true);
}
// 设置当前数据表和模型名
if (!empty($this->table)) {
$query->setTable($this->table);