Skip to content

Commit

Permalink
优化查询缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jun 25, 2024
1 parent 9226291 commit fed3f23
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/db/PDOConnection.php
Original file line number Diff line number Diff line change
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 @@ -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

0 comments on commit fed3f23

Please sign in to comment.