Db::name('user')->field('id,name')->cache(true, 60, 'user:1')->where('id', 1)->find();
Db::name('user')->field('id,name,tel,email,create_time')->cache(true, 60, 'user:1')->where('id', 1)->find(); 第二条查询得到的缓存数据和第一条是一样的。在文件thinkphp\library\think\db\Query.php 2466行
if (!is_null($data)) {
// AR模式分析主键条件
$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, $this->bind);
}/**
* 生成缓存标识
* @access public
* @param mixed $value 缓存数据
* @param array $options 缓存参数
* @param array $bind 绑定参数
* @return string
*/
protected function getCacheKey($value, $options, $bind = [])
{
if (is_scalar($value)) {
$data = $value;
} elseif (is_array($value) && is_string($value[0]) && 'eq' == strtolower($value[0])) {
$data = $value[1];
}
if (isset($data)) {
return 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;
} else {
return md5(serialize($options) . serialize($bind));
}
}find方法判断,如果查询条件中有主键id,则调用getCacheKey方法,指定缓存key为 think:表名称|主键
bug只在连贯操作find方法时出现
最佳答案