PushCache.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Cache\Push;
  3. use App\Consts\BaseConst;
  4. use App\Libs\Utils;
  5. use Illuminate\Support\Facades\Redis;
  6. class PushCache
  7. {
  8. /**
  9. * 获取用户reg id
  10. * @param $uid
  11. * @return mixed
  12. */
  13. public static function getUserPushRegId($uid)
  14. {
  15. $cacheKey = Utils::getCacheKey('push.user', [$uid]);
  16. return Redis::get($cacheKey);
  17. }
  18. /**
  19. * 设置用户reg id
  20. * @param $uid
  21. * @param $regId
  22. * @return mixed
  23. */
  24. public static function setUserPushRegId($uid, $regId)
  25. {
  26. $cacheKey = Utils::getCacheKey('push.user', [$uid]);
  27. return Redis::set($cacheKey, $regId);
  28. }
  29. /**
  30. * 增加pv
  31. * @param $pushId
  32. * @param $date
  33. * @return bool
  34. */
  35. public static function incrPushPv($pushId, $date): bool
  36. {
  37. if (empty($pushId) || empty($date)) {
  38. return false;
  39. }
  40. $cacheKey = Utils::getCacheKey('push.statsPv', [$pushId, $date]);
  41. Redis::incrBy($cacheKey, 1);
  42. $ttl = Redis::ttl($cacheKey);
  43. if ((int)$ttl < 1) {
  44. Redis::expire($cacheKey, BaseConst::ONE_DAY_SECONDS * 3);
  45. }
  46. return true;
  47. }
  48. /**
  49. * @param $pushId
  50. * @param $date
  51. * @param $uid
  52. * @return bool
  53. */
  54. public static function zAddPushUv($pushId, $date, $uid): bool
  55. {
  56. if (empty($pushId) || empty($date) || empty($uid)) {
  57. return false;
  58. }
  59. $cacheKey = Utils::getCacheKey('push.statsUv', [$pushId, $date]);
  60. Redis::zAdd($cacheKey, time(), $uid);
  61. $ttl = Redis::ttl($cacheKey);
  62. if ((int)$ttl < 1) {
  63. Redis::expire($cacheKey, BaseConst::ONE_DAY_SECONDS * 3);
  64. }
  65. return true;
  66. }
  67. /**
  68. * 获取pv数
  69. * @param $pushId
  70. * @param $date
  71. * @return int
  72. */
  73. public static function getPushyPv($pushId, $date): int
  74. {
  75. $cacheKey = Utils::getCacheKey('push.statsPv', [$pushId, $date]);
  76. $exist = Redis::exists($cacheKey);
  77. if (!$exist) {
  78. return 0;
  79. }
  80. return (int)Redis::get($cacheKey);
  81. }
  82. /**
  83. * 获取uv数
  84. * @param $pushId
  85. * @param $date
  86. * @return int
  87. */
  88. public static function getPushUv($pushId, $date): int
  89. {
  90. $cacheKey = Utils::getCacheKey('push.statsUv', [$pushId, $date]);
  91. $exist = Redis::exists($cacheKey);
  92. if (!$exist) {
  93. return 0;
  94. }
  95. return (int)Redis::zCount($cacheKey, 0, 99999999999999999999999999);
  96. }
  97. }