From fb8415e653ddb83dab28e7d853b6646e082870a2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 4 Feb 2017 14:13:44 +0800 Subject: [PATCH] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0cacheData?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E6=95=B0=E6=8D=AE=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=BB=9F=E4=B8=80=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 46 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 335fe493..3d080dcc 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -419,11 +419,7 @@ class Query } if (isset($cache)) { // 缓存数据 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($key, $result, $cache['expire']); - } else { - Cache::set($key, $result, $cache['expire']); - } + $this->cacheData($key, $result, $cache); } } else { // 清空查询条件 @@ -491,11 +487,7 @@ class Query } if (isset($cache) && isset($guid)) { // 缓存数据 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($guid, $result, $cache['expire']); - } else { - Cache::set($guid, $result, $cache['expire']); - } + $this->cacheData($guid, $result, $cache); } } else { // 清空查询条件 @@ -2308,11 +2300,7 @@ class Query if (isset($cache)) { // 缓存数据集 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($key, $resultSet, $cache['expire']); - } else { - Cache::set($key, $resultSet, $cache['expire']); - } + $this->cacheData($key, $resultSet, $cache); } } @@ -2356,6 +2344,28 @@ class Query return $resultSet; } + /** + * 缓存数据 + * @access public + * @param string $key 缓存标识 + * @param mixed $data 缓存数据 + * @param array $config 缓存参数 + */ + protected function cacheData($key, $data, $config = []) + { + if (isset($config['tag'])) { + Cache::tag($config['tag'])->set($key, $data, $config['expire']); + } else { + Cache::set($key, $data, $config['expire']); + } + } + + /** + * 生成缓存标识 + * @access public + * @param mixed $value 缓存数据 + * @param array $options 缓存参数 + */ protected function getCacheKey($value, $options) { if (is_scalar($value)) { @@ -2443,11 +2453,7 @@ class Query if (isset($cache)) { // 缓存数据 - if (isset($cache['tag'])) { - Cache::tag($cache['tag'])->set($key, $result, $cache['expire']); - } else { - Cache::set($key, $result, $cache['expire']); - } + $this->cacheData($key, $result, $cache); } }