ActivityCache.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Cache\Activity;
  3. use App\Consts\BaseConst;
  4. use App\Libs\Utils;
  5. use Illuminate\Support\Facades\Redis;
  6. class ActivityCache
  7. {
  8. public static function incrActivityPv($activityId, $date)
  9. {
  10. $cacheKey = Utils::getCacheKey('activity.statsPv', [$activityId, $date]);
  11. Redis::incrBy($cacheKey, 1);
  12. $ttl = Redis::ttl($cacheKey);
  13. if ((int)$ttl < 1) {
  14. Redis::expire($cacheKey, BaseConst::ONE_DAY_SECONDS * 3);
  15. }
  16. return true;
  17. }
  18. public static function zAddActivityUv($activityId, $date, $uid): bool
  19. {
  20. $cacheKey = Utils::getCacheKey('activity.statsUv', [$activityId, $date]);
  21. Redis::zAdd($cacheKey, time(), $uid);
  22. $ttl = Redis::ttl($cacheKey);
  23. if ((int)$ttl < 1) {
  24. Redis::expire($cacheKey, BaseConst::ONE_WEEK_SECONDS);
  25. }
  26. return true;
  27. }
  28. /**
  29. * 获取pv数
  30. * @param $activityId
  31. * @param $date
  32. * @return int
  33. */
  34. public static function getActivityPv($activityId, $date): int
  35. {
  36. $cacheKey = Utils::getCacheKey('activity.statsPv', [$activityId, $date]);
  37. $exist = Redis::exists($cacheKey);
  38. if (!$exist) {
  39. return 0;
  40. }
  41. return (int)Redis::get($cacheKey);
  42. }
  43. /**
  44. * 获取uv数
  45. * @param $activityId
  46. * @param $date
  47. * @return int
  48. */
  49. public static function getActivityUv($activityId, $date): int
  50. {
  51. $cacheKey = Utils::getCacheKey('activity.statsUv', [$activityId, $date]);
  52. $exist = Redis::exists($cacheKey);
  53. if (!$exist) {
  54. return 0;
  55. }
  56. return (int)Redis::zCount($cacheKey, 0, 99999999999999999999999999);
  57. }
  58. }