Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修正查询缓存 #527

Open
wants to merge 3 commits into
base: 3.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 51 additions & 49 deletions src/db/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,45 @@ abstract class PDOConnection extends Connection
*/
protected $config = [
// 数据库类型
'type' => '',
'type' => '',
// 服务器地址
'hostname' => '',
'hostname' => '',
// 数据库名
'database' => '',
'database' => '',
// 用户名
'username' => '',
'username' => '',
// 密码
'password' => '',
'password' => '',
// 端口
'hostport' => '',
'hostport' => '',
// 连接dsn
'dsn' => '',
'dsn' => '',
// 数据库连接参数
'params' => [],
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
'charset' => 'utf8',
// 数据库表前缀
'prefix' => '',
'prefix' => '',
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
'slave_no' => '',
// 模型写入后自动读取主服务器
'read_master' => false,
'read_master' => false,
// 是否严格检查字段是否存在
'fields_strict' => true,
'fields_strict' => true,
// 开启字段缓存
'fields_cache' => false,
'fields_cache' => false,
// 监听SQL
'trigger_sql' => true,
'trigger_sql' => true,
// Builder类
'builder' => '',
'builder' => '',
// Query类
'query' => '',
'query' => '',
// 是否需要断线重连
'break_reconnect' => false,
// 断线标识字符串
Expand Down Expand Up @@ -148,11 +148,11 @@ abstract class PDOConnection extends Connection
* @var array
*/
protected $params = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_EMULATE_PREPARES => false,
];

/**
Expand All @@ -161,14 +161,14 @@ abstract class PDOConnection extends Connection
* @var array
*/
protected $bindType = [
'string' => self::PARAM_STR,
'str' => self::PARAM_STR,
'integer' => self::PARAM_INT,
'int' => self::PARAM_INT,
'boolean' => self::PARAM_BOOL,
'bool' => self::PARAM_BOOL,
'float' => self::PARAM_FLOAT,
'datetime' => self::PARAM_STR,
'string' => self::PARAM_STR,
'str' => self::PARAM_STR,
'integer' => self::PARAM_INT,
'int' => self::PARAM_INT,
'boolean' => self::PARAM_BOOL,
'bool' => self::PARAM_BOOL,
'float' => self::PARAM_FLOAT,
'datetime' => self::PARAM_STR,
'timestamp' => self::PARAM_STR,
];

Expand Down Expand Up @@ -276,10 +276,10 @@ public function fieldCase(array $info): array
{
// 字段大小写转换
return match ($this->attrCase) {
PDO::CASE_LOWER => array_change_key_case($info),
PDO::CASE_UPPER => array_change_key_case($info, CASE_UPPER),
PDO::CASE_NATURAL => $info,
default => $info,
PDO::CASE_LOWER => array_change_key_case($info),
PDO::CASE_UPPER => array_change_key_case($info, CASE_UPPER),
PDO::CASE_NATURAL => $info,
default => $info,
};
}

Expand Down Expand Up @@ -389,10 +389,10 @@ public function getSchemaInfo(string $tableName, $force = false)
}

$this->info[$schema] = [
'fields' => array_keys($info),
'type' => $info,
'bind' => $bind,
'pk' => $pk,
'fields' => array_keys($info),
'type' => $info,
'bind' => $bind,
'pk' => $pk,
'autoinc' => $autoinc,
];
}
Expand Down Expand Up @@ -723,10 +723,8 @@ protected function pdoQuery(BaseQuery $query, $sql, bool $master = null): array
if (!$query->getOptions('force_cache')) {
$key = $cacheItem->getKey();

$data = $this->cache->get($key);

if (null !== $data) {
return $data;
if ($this->cache->has($key)) {
return $this->cache->get($key);
}
}
}
Expand Down Expand Up @@ -891,8 +889,8 @@ protected function pdoExecute(BaseQuery $query, string $sql, bool $origin = fals
*/
protected function queryPDOStatement(BaseQuery $query, string $sql): PDOStatement
{
$options = $query->getOptions();
$bind = $query->getBind();
$options = $query->getOptions();
$bind = $query->getBind();
$master = !empty($options['master']);
$procedure = !empty($options['procedure']) || in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);

Expand Down Expand Up @@ -1252,14 +1250,16 @@ public function value(BaseQuery $query, string $field, $default = null, bool $on
$pdo = $this->getPDOStatement($sql, $query->getBind(), $options['master']);

$result = $pdo->fetchColumn();
$result = false !== $result ? $result : $default;
$requireCache = $query->getOptions('cache_always') || !empty($result);

if (isset($cacheItem)) {
if (isset($cacheItem) && $requireCache) {
// 缓存数据
$cacheItem->set($result);
$this->cacheData($cacheItem);
}

return false !== $result ? $result : $default;
return $result;
}

/**
Expand Down Expand Up @@ -1374,7 +1374,9 @@ public function column(BaseQuery $query, string|array $column, string $key = '')
$result = $resultSet;
}

if (isset($cacheItem)) {
$requireCache = $query->getOptions('cache_always') || !empty($result);

if (isset($cacheItem) && $requireCache) {
// 缓存数据
$cacheItem->set($result);
$this->cacheData($cacheItem);
Expand Down