改进Query类的自动缓存更新

This commit is contained in:
thinkphp
2017-01-30 13:31:05 +08:00
parent 57ff2d9fc5
commit f12adbb943

View File

@@ -2187,8 +2187,8 @@ class Query
} else { } else {
$options['where']['AND'] = $where; $options['where']['AND'] = $where;
} }
} elseif (is_string($pk) && isset($options['where']['AND'][$pk]) && is_scalar($options['where']['AND'][$pk])) { } elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) {
$key = 'think:' . $options['table'] . '|' . $options['where']['AND'][$pk]; $key = $this->getCacheKey($options['where']['AND'][$pk], $options);
} }
// 生成UPDATE SQL语句 // 生成UPDATE SQL语句
$sql = $this->builder->update($data, $options); $sql = $this->builder->update($data, $options);
@@ -2339,6 +2339,18 @@ class Query
return $resultSet; return $resultSet;
} }
protected function getCacheKey($value, $options)
{
if (is_scalar($value)) {
$data = $value;
} elseif (is_array($value) && 'eq' == strtolower($value[0])) {
$data = $value[1];
}
if (isset($data)) {
return 'think:' . $options['table'] . '|' . $data;
}
}
/** /**
* 查找单条记录 * 查找单条记录
* @access public * @access public
@@ -2358,10 +2370,12 @@ class Query
} }
// 分析查询表达式 // 分析查询表达式
$options = $this->parseExpress(); $options = $this->parseExpress();
$pk = $this->getPk($options);
if (!is_null($data)) { if (!is_null($data)) {
// AR模式分析主键条件 // AR模式分析主键条件
$this->parsePkWhere($data, $options); $this->parsePkWhere($data, $options);
} elseif (!empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['AND'][$pk])) {
$key = $this->getCacheKey($options['where']['AND'][$pk], $options);
} }
$options['limit'] = 1; $options['limit'] = 1;
@@ -2371,7 +2385,7 @@ class Query
$cache = $options['cache']; $cache = $options['cache'];
if (true === $cache['key'] && !is_null($data) && !is_array($data)) { if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
$key = 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; $key = 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;
} else { } elseif (!isset($key)) {
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
} }
$result = Cache::get($key); $result = Cache::get($key);
@@ -2577,8 +2591,8 @@ class Query
} }
// AR模式分析主键条件 // AR模式分析主键条件
$this->parsePkWhere($data, $options); $this->parsePkWhere($data, $options);
} elseif (is_string($pk) && isset($options['where']['AND'][$pk]) && is_scalar($options['where']['AND'][$pk])) { } elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) {
$key = 'think:' . $options['table'] . '|' . $options['where']['AND'][$pk]; $key = $this->getCacheKey($options['where']['AND'][$pk], $options);
} }
if (true !== $data && empty($options['where'])) { if (true !== $data && empty($options['where'])) {