/**
* 查找单个对象记录,支持主键缓存获取
* @param mixed $condition
* @return mixed|null|static
*/
public static function findByCondition($condition)
{
if (isset($condition[static::$pk])) {
$cache_key = self::getCacheKey($condition[static::$pk], false);
Yii::trace('from cache object:' . $cache_key, __METHOD__);
if (self::allowFromCache($condition)) {
$row = Yii::$app->cache->get($cache_key);
if ($row) {
return $row;
}
}
}
$row = parent::findByCondition($condition);
if ($row && isset($condition[static::$pk]) && self::allowFromCache($condition)) {
if (Yii::$app->cache->exists($cache_key)) {
Yii::$app->cache->set($cache_key, $row, Yii::$app->params['ttl']);
} else {
Yii::$app->cache->add($cache_key, $row, Yii::$app->params['ttl']);
}
}
return $row;
}