xhActiveService.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\models\xhActive;
  6. class xhActiveService {
  7. public static function add($data)
  8. {
  9. $active = xhActive::add($data);
  10. $id = $active['id'];
  11. self::refreshById($id);
  12. return $active;
  13. }
  14. public static function refreshById($id)
  15. {
  16. $activeKey = dict::getCacheKey('xhActive').$id;
  17. Yii::$app->redis->executeCommand('DEL', [$activeKey]);
  18. self::getById($id);
  19. }
  20. public static function updateById($id,$data)
  21. {
  22. xhActive::updateById($id, $data);
  23. $activeKey = dict::getCacheKey('xhActive').$id;
  24. $activeParams = [$activeKey];
  25. foreach($data as $key => $val){
  26. $activeParams[] = $key;
  27. $activeParams[] = $val;
  28. }
  29. Yii::$app->redis->executeCommand('HMSET',$activeParams);//更新缓存信息
  30. }
  31. public static function getById($id)
  32. {
  33. $cacheKey = dict::getCacheKey('xhActive').$id;
  34. $active = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  35. if(!empty($active)){
  36. $arr = [];
  37. $i = 0;
  38. $x = 0;
  39. foreach($active as $key => $val){
  40. $i++;
  41. if($i%2 == 0){
  42. $arr[$x]['value'] = $val;
  43. $x++;
  44. }else{
  45. $arr[$x]['key'] = $val;
  46. }
  47. }
  48. $activeArr = [];
  49. foreach($arr as $key => $val){
  50. $activeArr[$val['key']] = $val['value'];
  51. }
  52. unset($activeArr['info_already_get_by_sql']);
  53. return $activeArr;
  54. }
  55. $active = xhActive::find()->where(['id' => $id])->asArray()->one();
  56. $params = [$cacheKey];
  57. $params[] = 'info_already_get_by_sql';
  58. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  59. if(!empty($active)){
  60. foreach($active as $key => $val){
  61. $params[] = $key;
  62. $params[] = $val;
  63. }
  64. }
  65. Yii::$app->redis->executeCommand('HMSET',$params);
  66. return $active;
  67. }
  68. }