实现请求缓存1天。

This commit is contained in:
2025-04-18 15:38:58 +08:00
parent 2d3f7d8511
commit 96da83a1ab
209 changed files with 19400 additions and 1657 deletions

View File

@@ -622,15 +622,14 @@ abstract class PDOConnection extends Connection
* @access public
* @param BaseQuery $query 查询对象
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param Model|null $model 模型对象实例
* @param null $condition 查询条件
* @return \Generator
* @throws DbException
*/
public function getCursor(BaseQuery $query, string $sql, array $bind = [], $model = null, $condition = null)
public function getCursor(BaseQuery $query, string $sql, $model = null, $condition = null)
{
$this->queryPDOStatement($query, $sql, $bind);
$this->queryPDOStatement($query, $sql);
// 返回结果集
while ($result = $this->PDOStatement->fetch($this->fetchType)) {
@@ -653,7 +652,7 @@ abstract class PDOConnection extends Connection
*/
public function query(string $sql, array $bind = [], bool $master = false): array
{
return $this->pdoQuery($this->newQuery(), $sql, $bind, $master);
return $this->pdoQuery($this->newQuery()->bind($bind), $sql, $master);
}
/**
@@ -666,7 +665,7 @@ abstract class PDOConnection extends Connection
*/
public function execute(string $sql, array $bind = []): int
{
return $this->pdoExecute($this->newQuery(), $sql, $bind, true);
return $this->pdoExecute($this->newQuery()->bind($bind), $sql, true);
}
/**
@@ -674,25 +673,27 @@ abstract class PDOConnection extends Connection
* @access protected
* @param BaseQuery $query 查询对象
* @param mixed $sql sql指令
* @param array $bind 参数绑定
* @param bool $master 主库读取
* @return array
* @throws DbException
*/
protected function pdoQuery(BaseQuery $query, $sql, array $bind = [], bool $master = null): array
protected function pdoQuery(BaseQuery $query, $sql, bool $master = null): array
{
// 分析查询表达式
$query->parseOptions();
$bind = $query->getBind();
if ($query->getOptions('cache')) {
// 检查查询缓存
$cacheItem = $this->parseCache($query, $query->getOptions('cache'));
$key = $cacheItem->getKey();
if (!$query->getOptions('force_cache')) {
$key = $cacheItem->getKey();
$data = $this->cache->get($key);
$data = $this->cache->get($key);
if (null !== $data) {
return $data;
if (null !== $data) {
return $data;
}
}
}
@@ -730,11 +731,10 @@ abstract class PDOConnection extends Connection
*/
public function pdo(BaseQuery $query): PDOStatement
{
$bind = $query->getBind();
// 生成查询SQL
$sql = $this->builder->select($query);
return $this->queryPDOStatement($query, $sql, $bind);
return $this->queryPDOStatement($query, $sql);
}
/**
@@ -806,18 +806,17 @@ abstract class PDOConnection extends Connection
* @access protected
* @param BaseQuery $query 查询对象
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param bool $origin 是否原生查询
* @return int
* @throws DbException
*/
protected function pdoExecute(BaseQuery $query, string $sql, array $bind = [], bool $origin = false): int
protected function pdoExecute(BaseQuery $query, string $sql, bool $origin = false): int
{
if ($origin) {
$query->parseOptions();
}
$this->queryPDOStatement($query->master(true), $sql, $bind);
$this->queryPDOStatement($query->master(true), $sql);
if (!$origin && !empty($this->config['deploy']) && !empty($this->config['read_master'])) {
$this->readMaster = true;
@@ -844,13 +843,13 @@ abstract class PDOConnection extends Connection
/**
* @param BaseQuery $query
* @param string $sql
* @param array $bind
* @return PDOStatement
* @throws DbException
*/
protected function queryPDOStatement(BaseQuery $query, string $sql, array $bind = []): PDOStatement
protected function queryPDOStatement(BaseQuery $query, string $sql): PDOStatement
{
$options = $query->getOptions();
$bind = $query->getBind();
$master = !empty($options['master']) ? true : false;
$procedure = !empty($options['procedure']) ? true : in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);
@@ -898,7 +897,7 @@ abstract class PDOConnection extends Connection
$condition = $options['where']['AND'] ?? null;
// 执行查询操作
return $this->getCursor($query, $sql, $query->getBind(), $query->getModel(), $condition);
return $this->getCursor($query, $sql, $query->getModel(), $condition);
}
/**
@@ -938,7 +937,7 @@ abstract class PDOConnection extends Connection
$sql = $this->builder->insert($query);
// 执行操作
$result = '' == $sql ? 0 : $this->pdoExecute($query, $sql, $query->getBind());
$result = '' == $sql ? 0 : $this->pdoExecute($query, $sql);
if ($result) {
$sequence = $options['sequence'] ?? null;
@@ -977,13 +976,12 @@ abstract class PDOConnection extends Connection
*/
public function insertAll(BaseQuery $query, array $dataSet = [], int $limit = 0): int
{
$query->parseOptions();
if (!is_array(reset($dataSet))) {
return 0;
}
$options = $query->parseOptions();
$replace = !empty($options['replace']);
if (0 === $limit && count($dataSet) >= 5000) {
$limit = 1000;
}
@@ -997,8 +995,8 @@ abstract class PDOConnection extends Connection
$count = 0;
foreach ($array as $item) {
$sql = $this->builder->insertAll($query, $item, $replace);
$count += $this->pdoExecute($query, $sql, $query->getBind());
$sql = $this->builder->insertAll($query, $item);
$count += $this->pdoExecute($query, $sql);
}
// 提交事务
@@ -1011,9 +1009,56 @@ abstract class PDOConnection extends Connection
return $count;
}
$sql = $this->builder->insertAll($query, $dataSet, $replace);
$sql = $this->builder->insertAll($query, $dataSet);
return $this->pdoExecute($query, $sql, $query->getBind());
return $this->pdoExecute($query, $sql);
}
/**
* 批量插入记录
* @access public
* @param BaseQuery $query 查询对象
* @param array $keys 键值
* @param array $values 数据
* @param integer $limit 每次写入数据限制
* @return integer
* @throws \Exception
* @throws \Throwable
*/
public function insertAllByKeys(BaseQuery $query, array $keys, array $values, int $limit = 0): int
{
$query->parseOptions();
if (0 === $limit && count($values) >= 5000) {
$limit = 1000;
}
if ($limit) {
// 分批写入 自动启动事务支持
$this->startTrans();
try {
$array = array_chunk($values, $limit, true);
$count = 0;
foreach ($array as $item) {
$sql = $this->builder->insertAllByKeys($query, $keys, $item);
$count += $this->pdoExecute($query, $sql);
}
// 提交事务
$this->commit();
} catch (\Exception | \Throwable $e) {
$this->rollback();
throw $e;
}
return $count;
}
$sql = $this->builder->insertAllByKeys($query, $keys, $values);
return $this->pdoExecute($query, $sql);
}
/**
@@ -1032,7 +1077,7 @@ abstract class PDOConnection extends Connection
$sql = $this->builder->selectInsert($query, $fields, $table);
return $this->pdoExecute($query, $sql, $query->getBind());
return $this->pdoExecute($query, $sql);
}
/**
@@ -1050,7 +1095,7 @@ abstract class PDOConnection extends Connection
$sql = $this->builder->update($query);
// 执行操作
$result = '' == $sql ? 0 : $this->pdoExecute($query, $sql, $query->getBind());
$result = '' == $sql ? 0 : $this->pdoExecute($query, $sql);
if ($result) {
$this->db->trigger('after_update', $query);
@@ -1075,7 +1120,7 @@ abstract class PDOConnection extends Connection
$sql = $this->builder->delete($query);
// 执行操作
$result = $this->pdoExecute($query, $sql, $query->getBind());
$result = $this->pdoExecute($query, $sql);
if ($result) {
$this->db->trigger('after_delete', $query);
@@ -1109,10 +1154,13 @@ abstract class PDOConnection extends Connection
if (!empty($options['cache'])) {
$cacheItem = $this->parseCache($query, $options['cache'], 'value');
$key = $cacheItem->getKey();
if ($this->cache->has($key)) {
return $this->cache->get($key);
if (!$query->getOptions('force_cache')) {
$key = $cacheItem->getKey();
if ($this->cache->has($key)) {
return $this->cache->get($key);
}
}
}
@@ -1208,10 +1256,12 @@ abstract class PDOConnection extends Connection
if (!empty($options['cache'])) {
// 判断查询缓存
$cacheItem = $this->parseCache($query, $options['cache'], 'column');
$name = $cacheItem->getKey();
if (!$query->getOptions('force_cache')) {
$name = $cacheItem->getKey();
if ($this->cache->has($name)) {
return $this->cache->get($name);
if ($this->cache->has($name)) {
return $this->cache->get($name);
}
}
}
@@ -1545,17 +1595,16 @@ abstract class PDOConnection extends Connection
* @access public
* @param BaseQuery $query 查询对象
* @param array $sqlArray SQL批处理指令
* @param array $bind 参数绑定
* @return bool
*/
public function batchQuery(BaseQuery $query, array $sqlArray = [], array $bind = []): bool
public function batchQuery(BaseQuery $query, array $sqlArray = []): bool
{
// 自动启动事务支持
$this->startTrans();
try {
foreach ($sqlArray as $sql) {
$this->pdoExecute($query, $sql, $bind);
$this->pdoExecute($query, $sql);
}
// 提交事务
$this->commit();