HomeCache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * ${CARET}
  4. * @file:HomeCache.php
  5. * @Created by gnitif
  6. * @Date: 2022/12/6
  7. * @Time: 17:37
  8. */
  9. namespace App\Cache\Home;
  10. use App\Libs\Utils;
  11. use Illuminate\Support\Facades\Redis;
  12. class HomeCache
  13. {
  14. /**
  15. * 获取首页数据缓存
  16. * name: getHomePageInfo
  17. * @param mixed $packageName 包名
  18. * @param mixed $sex 性别
  19. * @return mixed
  20. * date 2022/12/06 17:43
  21. */
  22. public static function getHomePageInfo($packageName, $sex)
  23. {
  24. $cacheKey = Utils::getCacheKey("home.page", [$packageName]);
  25. $data = Redis::hget($cacheKey, $sex);
  26. if ($data) {
  27. $data = json_decode($data, true);
  28. }
  29. return $data;
  30. }
  31. /**
  32. * 首页数据缓存
  33. * name: setHomePageInfo
  34. * @param mixed $packageName 包名
  35. * @param mixed $sex 性别
  36. * @param mixed $data 数据
  37. * @param int $seconds 有效期
  38. * date 2022/12/07 10:59
  39. */
  40. public static function setHomePageInfo($packageName, $sex, $data, $seconds = 0)
  41. {
  42. $cacheKey = Utils::getCacheKey("home.page", [$packageName]);
  43. self::setHashData($cacheKey, $sex, $data, $seconds);
  44. }
  45. /**
  46. * 排行榜数据缓存
  47. * name: setHomePageInfo
  48. * @param mixed $packageName 包名
  49. * @param mixed $sex 性别
  50. * @param mixed $data 数据
  51. * @param int $seconds 有效期(秒)
  52. * date 2022/12/07 10:59
  53. */
  54. public static function setRankPageInfo($packageName, $sex, $data, $seconds = 0)
  55. {
  56. $cacheKey = Utils::getCacheKey("home.rank", [$packageName]);
  57. self::setHashData($cacheKey, $sex, $data, $seconds);
  58. }
  59. /**
  60. * 获取排行榜数据缓存
  61. * name: getHomePageInfo
  62. * @param mixed $packageName 包名
  63. * @param mixed $sex 性别
  64. * @return mixed
  65. * date 2022/12/06 17:43
  66. */
  67. public static function getRankPageInfo($packageName, $sex)
  68. {
  69. $cacheKey = Utils::getCacheKey("home.rank", [$packageName]);
  70. return self::getHashData($cacheKey,$sex);
  71. }
  72. /**
  73. * 设置缓存数据
  74. * name: setData
  75. * @param mixed $cacheKey 键值
  76. * @param mixed $field 字段
  77. * @param array $data 数据
  78. * @param int $seconds 有效期(秒)
  79. * date 2022/12/07 11:13
  80. */
  81. protected static function setHashData($cacheKey, $field, $data, $seconds = 0)
  82. {
  83. Redis::hset($cacheKey, $field, json_encode($data));
  84. if ($seconds > 0) {
  85. Redis::expire($cacheKey, $seconds);
  86. }
  87. }
  88. /**
  89. *
  90. * name: getData
  91. * @param mixed $cacheKey 键值
  92. * @param mixed $field 字段
  93. * @return mixed
  94. * date 2022/12/07 11:15
  95. */
  96. protected static function getHashData($cacheKey, $field)
  97. {
  98. $data = Redis::hget($cacheKey, $field);
  99. if ($data) {
  100. $data = json_decode($data, true);
  101. }
  102. return $data;
  103. }
  104. }