xhUnionUserService.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\components\stringUtil;
  6. use common\models\xhUnionUser;
  7. class xhUnionUserService {
  8. public static function add($data)
  9. {
  10. xhUnionUser::add($data);
  11. }
  12. public static function getById($unionId)
  13. {
  14. $cacheKey = dict::getCacheKey('xhUnionUser').$unionId;
  15. $user = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  16. if(!empty($user)){
  17. $arr = [];
  18. $i = 0;
  19. $x = 0;
  20. foreach($user as $key => $val){
  21. $i++;
  22. if($i%2 == 0){
  23. $arr[$x]['value'] = $val;
  24. $x++;
  25. }else{
  26. $arr[$x]['key'] = $val;
  27. }
  28. }
  29. $userArr = [];
  30. foreach($arr as $key => $val){
  31. $userArr[$val['key']] = $val['value'];
  32. }
  33. return $userArr;
  34. }
  35. $user = xhUnionUser::find()->where(['unionId' => $unionId])->asArray()->one();
  36. if(empty($user)){
  37. return [];
  38. }
  39. $params = [$cacheKey];
  40. foreach($user as $key => $val){
  41. $params[] = $key;
  42. $params[] = $val;
  43. }
  44. Yii::$app->redis->executeCommand('HMSET',$params);//将用户信息、用户标签放入缓存
  45. return $user;
  46. }
  47. public static function refreshById($unionId)
  48. {
  49. $cacheKey = dict::getCacheKey('xhUnionUser').$unionId;
  50. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  51. return self::getById($unionId);
  52. }
  53. public static function updateById($unionId,$data)
  54. {
  55. xhUnionUser::updateByCondition(['unionId' => $unionId], $data);
  56. $userKey = dict::getCacheKey('xhUnionUser').$unionId;
  57. $userParams = [$userKey];
  58. foreach($data as $key => $val){
  59. $userParams[] = $key;
  60. $userParams[] = $val;
  61. }
  62. Yii::$app->redis->executeCommand('HMSET',$userParams);//更新缓存信息
  63. }
  64. }