GroupsLikeService.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Services\Groups;
  3. use App\Util\ActivityPub\Helpers;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Redis;
  6. use App\Models\GroupLike;
  7. class GroupsLikeService
  8. {
  9. const CACHE_KEY = 'pf:services:group-likes:ids:';
  10. const CACHE_SET_KEY = 'pf:services:group-likes:set:';
  11. const CACHE_POST_KEY = 'pf:services:group-likes:count:';
  12. public static function add($profileId, $statusId)
  13. {
  14. $key = self::CACHE_KEY . $profileId . ':' . $statusId;
  15. Cache::increment(self::CACHE_POST_KEY . $statusId);
  16. //Cache::forget('pf:services:likes:liked_by:'.$statusId);
  17. self::setAdd($profileId, $statusId);
  18. return Cache::put($key, true, 86400);
  19. }
  20. public static function setAdd($profileId, $statusId)
  21. {
  22. if(self::setCount($profileId) > 400) {
  23. Redis::zpopmin(self::CACHE_SET_KEY . $profileId);
  24. }
  25. return Redis::zadd(self::CACHE_SET_KEY . $profileId, $statusId, $statusId);
  26. }
  27. public static function setCount($id)
  28. {
  29. return Redis::zcard(self::CACHE_SET_KEY . $id);
  30. }
  31. public static function setRem($profileId, $val)
  32. {
  33. return Redis::zrem(self::CACHE_SET_KEY . $profileId, $val);
  34. }
  35. public static function get($profileId, $start = 0, $stop = 10)
  36. {
  37. if($stop > 100) {
  38. $stop = 100;
  39. }
  40. return Redis::zrevrange(self::CACHE_SET_KEY . $profileId, $start, $stop);
  41. }
  42. public static function remove($profileId, $statusId)
  43. {
  44. $key = self::CACHE_KEY . $profileId . ':' . $statusId;
  45. Cache::decrement(self::CACHE_POST_KEY . $statusId);
  46. //Cache::forget('pf:services:likes:liked_by:'.$statusId);
  47. self::setRem($profileId, $statusId);
  48. return Cache::put($key, false, 86400);
  49. }
  50. public static function liked($profileId, $statusId)
  51. {
  52. $key = self::CACHE_KEY . $profileId . ':' . $statusId;
  53. return Cache::remember($key, 900, function() use($profileId, $statusId) {
  54. return GroupLike::whereProfileId($profileId)->whereStatusId($statusId)->exists();
  55. });
  56. }
  57. public static function likedBy($status)
  58. {
  59. $empty = [
  60. 'username' => null,
  61. 'others' => false
  62. ];
  63. return $empty;
  64. }
  65. public static function count($id)
  66. {
  67. return Cache::get(self::CACHE_POST_KEY . $id, 0);
  68. }
  69. }