yii2 redis使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii2 redis使用相关的知识,希望对你有一定的参考价值。
$redis = Yii::$app->redis;
$keys = ‘dll_mb_examine‘;//定义键名
$list = $redis->get($keys);//取key值
if(empty($list)){
$data=Examine::getMobileExamine($userInfo[‘userId‘]);
if(empty($data)){
return $this->redirect([‘/frontend/default/nomsg‘,‘msg‘=>‘暂无数据‘,‘title‘=>‘审批中心‘]);
}
$redis->set($keys,serialize($data));//存数据
$redis->expire($keys,3600*5);//缓存时间5秒
}
$data=unserialize($list);
$spdata=Config::getConfig(‘shenpileixing‘);
foreach($data as $key=>$val){
//编辑数据的时候更新redis缓存
$key=Yii::$app->params[‘cache_key‘][‘cyj_info‘].$data[‘earnestId‘];
if(!empty($key)){
Yii::$app->cache->delete($key);
}
web.php文件里引入redis
‘components‘ => [
‘request‘ => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
‘cookieValidationKey‘ => ‘pQwkcmQwaDnwv2kQIEpshK2XCYfAilIf‘,
],
‘assetManager‘ => [
‘linkAssets‘ => true,
],
‘cache‘ => [
// ‘class‘ => ‘yii\caching\FileCache‘,
‘class‘ => ‘yii\redis\Cache‘,
],
‘redis‘ => [
‘class‘ => ‘yii\redis\Connection‘,
‘hostname‘ => ‘192.168.0.227‘,
‘port‘ => 6379,
‘database‘ => 0,
],
yiisoft/extensions.php 尾部追加:
‘yiisoft/yii2-redis‘ =>
array (
‘name‘ => ‘yiisoft/yii2-redis‘,
‘version‘ => ‘2.2.0.0‘,
‘alias‘ =>
array (
‘@yii/redis‘ => $vendorDir . ‘/yiisoft/yii2-redis‘,
),
),
本文出自 “PHP/[email protected]” 博客,请务必保留此出处http://liang3391.blog.51cto.com/178205/1883090
以上是关于yii2 redis使用的主要内容,如果未能解决你的问题,请参考以下文章