query类value和column方法支持缓存

This commit is contained in:
thinkphp
2016-04-21 11:32:54 +08:00
parent 1b57af0d3f
commit f24bc5289c

View File

@@ -92,9 +92,22 @@ class Query
*/ */
public function value($field) public function value($field)
{ {
// 返回数据个数 $result = false;
if (!empty($this->options['cache'])) {
// 判断查询缓存
$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(); $pdo = $this->field($field)->fetchPdo(true)->find();
return $pdo->fetchColumn(); $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 = '')
{ {
$result = false;
if (!empty($this->options['cache'])) {
// 判断查询缓存
$cache = $this->options['cache'];
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options));
$result = Cache::get($guid);
}
if (!$result) {
$key = $key ? $key . ',' : ''; $key = $key ? $key . ',' : '';
$pdo = $this->field($key . $field)->fetchPdo(true)->select(); $pdo = $this->field($key . $field)->fetchPdo(true)->select();
if (1 == $pdo->columnCount()) { if (1 == $pdo->columnCount()) {
return $pdo->fetchAll(PDO::FETCH_COLUMN); $result = $pdo->fetchAll(PDO::FETCH_COLUMN);
} } else {
$result = $pdo->fetchAll(PDO::FETCH_ASSOC); $resultSet = $pdo->fetchAll(PDO::FETCH_ASSOC);
$fields = array_keys($result[0]); $fields = array_keys($resultSet[0]);
$count = count($fields); $count = count($fields);
$key1 = array_shift($fields); $key1 = array_shift($fields);
$key2 = $fields ? array_shift($fields) : ''; $key2 = $fields ? array_shift($fields) : '';
foreach ($result as $val) { foreach ($resultSet as $val) {
if ($count > 2) { if ($count > 2) {
$array[$val[$key1]] = $val; $result[$val[$key1]] = $val;
} elseif (2 == $count) { } elseif (2 == $count) {
$array[$val[$key1]] = $val[$key2]; $result[$val[$key1]] = $val[$key2];
} }
} }
return $array; }
if (isset($cache)) {
// 缓存数据
Cache::set($guid, $result, $cache['expire']);
}
}
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));