Skip to main content

PHP多级缓存的使用

背景

本实例基于Yii2框架

redis

安装redis扩展

composer require yiisoft/yii2-redis

yac

安装yac扩展

composer require diszz/yii2-phpyac

应用


public static function getObjectById($userId, $reset = false) {
return Yii::$app->yac->getOrSet('user:object:'.$userId, function() use ($userId, $reset){
return Yii::$app->redis->getOrSet('user:object:'.$userId, function() use ($userId){
$userObject = User::findIdentity($userId);

$_logs = ['$userId' => $userId];
Yii::error(__CLASS__.' '.__FUNCTION__.' '.__LINE__.' succ '.json_encode($_logs));
return $userObject;
}, 1800, $reset);
}, 60, $reset);
}