首页 > 代码库 > yii2 redis使用

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/Linux@HeFei” 博客,请务必保留此出处http://liang3391.blog.51cto.com/178205/1883090

yii2 redis使用