From 0aa1994ce334916bb889191b4026d0d157a085c5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 29 Jun 2016 18:05:18 +0800 Subject: [PATCH] =?UTF-8?q?Query=E7=B1=BB=E6=94=B9=E8=BF=9B=20cache?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=E8=87=AA=E5=8A=A8=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE=EF=BC=88=E4=BB=85?= =?UTF-8?q?=E9=92=88=E5=AF=B9=E4=B8=BB=E9=94=AE=E6=9F=A5=E8=AF=A2=E3=80=81?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=92=8C=E5=88=A0=E9=99=A4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 95213988..49604ac0 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1666,6 +1666,7 @@ class Query // 如果存在主键数据 则自动作为更新条件 if (is_string($pk) && isset($data[$pk])) { $where[$pk] = $data[$pk]; + $key = 'think:' . $options['table'] . '|' . $data[$pk]; unset($data[$pk]); } elseif (is_array($pk)) { // 增加复合主键支持 @@ -1692,6 +1693,11 @@ class Query // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $this->bind); } else { + // 检测缓存 + if (isset($key) && Cache::get($key)) { + // 删除缓存 + Cache::rm($key); + } // 执行操作 return '' == $sql ? 0 : $this->execute($sql, $this->getBind()); } @@ -1813,8 +1819,12 @@ class Query $result = false; if (empty($options['fetch_sql']) && !empty($options['cache'])) { // 判断查询缓存 - $cache = $options['cache']; - $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); + $cache = $options['cache']; + if (true === $cache['key'] && !is_array($data)) { + $key = 'think:' . $options['table'] . '|' . $data; + } else { + $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); + } $result = Cache::get($key); } if (!$result) { @@ -1964,6 +1974,10 @@ class Query $options = $this->parseExpress(); if (!is_null($data) && true !== $data) { + if (!is_array($data)) { + // 缓存标识 + $key = 'think:' . $options['table'] . '|' . $data; + } // AR模式分析主键条件 $this->parsePkWhere($data, $options); } @@ -1974,10 +1988,17 @@ class Query } // 生成删除SQL语句 $sql = $this->builder()->delete($options); + if ($options['fetch_sql']) { // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $this->bind); } + + // 检测缓存 + if (isset($key) && Cache::get($key)) { + // 删除缓存 + Cache::rm($key); + } // 执行操作 return $this->execute($sql, $this->getBind()); }