| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\components\stringUtil;
- use common\models\xhUnionUser;
- class xhUnionUserService {
- public static function add($data)
- {
- xhUnionUser::add($data);
- }
- public static function getById($unionId)
- {
- $cacheKey = dict::getCacheKey('xhUnionUser').$unionId;
- $user = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($user)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($user as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $userArr = [];
- foreach($arr as $key => $val){
- $userArr[$val['key']] = $val['value'];
- }
- return $userArr;
- }
- $user = xhUnionUser::find()->where(['unionId' => $unionId])->asArray()->one();
- if(empty($user)){
- return [];
- }
- $params = [$cacheKey];
- foreach($user as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- Yii::$app->redis->executeCommand('HMSET',$params);//将用户信息、用户标签放入缓存
- return $user;
- }
- public static function refreshById($unionId)
- {
- $cacheKey = dict::getCacheKey('xhUnionUser').$unionId;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- return self::getById($unionId);
- }
- public static function updateById($unionId,$data)
- {
- xhUnionUser::updateByCondition(['unionId' => $unionId], $data);
- $userKey = dict::getCacheKey('xhUnionUser').$unionId;
- $userParams = [$userKey];
- foreach($data as $key => $val){
- $userParams[] = $key;
- $userParams[] = $val;
- }
- Yii::$app->redis->executeCommand('HMSET',$userParams);//更新缓存信息
- }
- }
|