mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
query类value和column方法支持缓存
This commit is contained in:
@@ -92,9 +92,22 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function value($field)
|
public function value($field)
|
||||||
{
|
{
|
||||||
// 返回数据个数
|
$result = false;
|
||||||
$pdo = $this->field($field)->fetchPdo(true)->find();
|
if (!empty($this->options['cache'])) {
|
||||||
return $pdo->fetchColumn();
|
// 判断查询缓存
|
||||||
|
$cache = $this->options['cache'];
|
||||||
|
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options));
|
||||||
|
$result = Cache::get($key);
|
||||||
|
}
|
||||||
|
if (!$result) {
|
||||||
|
$pdo = $this->field($field)->fetchPdo(true)->find();
|
||||||
|
$result = $pdo->fetchColumn();
|
||||||
|
if (isset($cache)) {
|
||||||
|
// 缓存数据
|
||||||
|
Cache::set($key, $result, $cache['expire']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,24 +119,38 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function column($field, $key = '')
|
public function column($field, $key = '')
|
||||||
{
|
{
|
||||||
$key = $key ? $key . ',' : '';
|
$result = false;
|
||||||
$pdo = $this->field($key . $field)->fetchPdo(true)->select();
|
if (!empty($this->options['cache'])) {
|
||||||
if (1 == $pdo->columnCount()) {
|
// 判断查询缓存
|
||||||
return $pdo->fetchAll(PDO::FETCH_COLUMN);
|
$cache = $this->options['cache'];
|
||||||
|
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options));
|
||||||
|
$result = Cache::get($guid);
|
||||||
}
|
}
|
||||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
if (!$result) {
|
||||||
$fields = array_keys($result[0]);
|
$key = $key ? $key . ',' : '';
|
||||||
$count = count($fields);
|
$pdo = $this->field($key . $field)->fetchPdo(true)->select();
|
||||||
$key1 = array_shift($fields);
|
if (1 == $pdo->columnCount()) {
|
||||||
$key2 = $fields ? array_shift($fields) : '';
|
$result = $pdo->fetchAll(PDO::FETCH_COLUMN);
|
||||||
foreach ($result as $val) {
|
} else {
|
||||||
if ($count > 2) {
|
$resultSet = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$array[$val[$key1]] = $val;
|
$fields = array_keys($resultSet[0]);
|
||||||
} elseif (2 == $count) {
|
$count = count($fields);
|
||||||
$array[$val[$key1]] = $val[$key2];
|
$key1 = array_shift($fields);
|
||||||
|
$key2 = $fields ? array_shift($fields) : '';
|
||||||
|
foreach ($resultSet as $val) {
|
||||||
|
if ($count > 2) {
|
||||||
|
$result[$val[$key1]] = $val;
|
||||||
|
} elseif (2 == $count) {
|
||||||
|
$result[$val[$key1]] = $val[$key2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isset($cache)) {
|
||||||
|
// 缓存数据
|
||||||
|
Cache::set($guid, $result, $cache['expire']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $array;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1056,7 +1083,7 @@ class Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
$resultSet = false;
|
$resultSet = false;
|
||||||
if (isset($options['cache'])) {
|
if (!empty($options['cache'])) {
|
||||||
// 判断查询缓存
|
// 判断查询缓存
|
||||||
$cache = $options['cache'];
|
$cache = $options['cache'];
|
||||||
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
||||||
@@ -1130,7 +1157,7 @@ class Query
|
|||||||
|
|
||||||
$options['limit'] = 1;
|
$options['limit'] = 1;
|
||||||
$result = false;
|
$result = false;
|
||||||
if (isset($options['cache'])) {
|
if (!empty($options['cache'])) {
|
||||||
// 判断查询缓存
|
// 判断查询缓存
|
||||||
$cache = $options['cache'];
|
$cache = $options['cache'];
|
||||||
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
||||||
|
|||||||
Reference in New Issue
Block a user