$year,'month'=>$month,'riseNum'=>$userNum,'sjId'=>$sjId,'createTime'=>$createTime,'totalNum'=>$totalNum]; self::add($data); }else{ $newRiseNum = stringUtil::calcAdd($monthUser['riseNum'], $userNum); $data = ['riseNum'=>$newRiseNum,'totalNum'=>$totalNum]; self::updateByIds($sjId, $year,$month, $data); } } public static function updateByIds($sjId, $year,$month,$data) { xhUserByMonth::updateByCondition(['year'=>$year,'month'=>$month,'sjId'=>$sjId], $data); self::getByIds($sjId, $year, $month); $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month; $params = [$cacheKey]; if(!empty($data)){ foreach($data as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET',$params); return self::getByIds($sjId, $year,$month); } public static function add($data) { $return = xhUserByMonth::add($data); $sjId = $return['sjId']; $year = $return['year']; $month = $return['month']; $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month; $params = [$cacheKey]; if(!empty($return)){ foreach($return as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET',$params); return $return; } public static function getByIds($sjId, $year,$month) { $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month; $monthUser = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]); if(!empty($monthUser)){ $arr = []; $i = 0; $x = 0; foreach($monthUser as $key => $val){ $i++; if($i%2 == 0){ $arr[$x]['value'] = $val; $x++; }else{ $arr[$x]['key'] = $val; } } $monthUserArr = []; foreach($arr as $key => $val){ $monthUserArr[$val['key']] = $val['value']; } unset($monthUserArr['info_already_get_by_sql']); return $monthUserArr; } $monthUser = xhUserByMonth::find()->where(['sjId'=>$sjId,'year'=>$year,'month'=>$month])->asArray()->one(); $params = [$cacheKey]; $params[] = 'info_already_get_by_sql'; $params[] = 'ok'; if(!empty($monthUser)){ foreach($monthUser as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET',$params); return $monthUser; } /** * 获取本月粉丝数 */ public static function getMonthUser($sjId,$year,$month) { $monthUser = self::getByIds($sjId, $year, $month); $num = 0; if(!empty($monthUser)){ $num = $monthUser['riseNum']; } return $num; } }