NotificationService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\{
  6. Notification,
  7. Profile
  8. };
  9. use App\Transformer\Api\NotificationTransformer;
  10. use League\Fractal;
  11. use League\Fractal\Serializer\ArraySerializer;
  12. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  13. class NotificationService {
  14. const CACHE_KEY = 'pf:services:notifications:ids:';
  15. public static function get($id, $start = 0, $stop = 400)
  16. {
  17. $res = collect([]);
  18. $key = self::CACHE_KEY . $id;
  19. $stop = $stop > 400 ? 400 : $stop;
  20. $ids = Redis::zrangebyscore($key, $start, $stop);
  21. if(empty($ids)) {
  22. $ids = self::coldGet($id, $start, $stop);
  23. }
  24. foreach($ids as $id) {
  25. $res->push(self::getNotification($id));
  26. }
  27. return $res;
  28. }
  29. public static function coldGet($id, $start = 0, $stop = 400)
  30. {
  31. $stop = $stop > 400 ? 400 : $stop;
  32. $ids = Notification::whereProfileId($id)
  33. ->latest()
  34. ->skip($start)
  35. ->take($stop)
  36. ->pluck('id');
  37. foreach($ids as $key) {
  38. self::set($id, $key);
  39. }
  40. return $ids;
  41. }
  42. public static function getMax($id = false, $start = 0, $limit = 10)
  43. {
  44. $ids = self::getRankedMaxId($id, $start, $limit);
  45. if(empty($ids)) {
  46. return [];
  47. }
  48. $res = collect([]);
  49. foreach($ids as $id) {
  50. $res->push(self::getNotification($id));
  51. }
  52. return $res->toArray();
  53. }
  54. public static function getMin($id = false, $start = 0, $limit = 10)
  55. {
  56. $ids = self::getRankedMinId($id, $start, $limit);
  57. if(empty($ids)) {
  58. return [];
  59. }
  60. $res = collect([]);
  61. foreach($ids as $id) {
  62. $res->push(self::getNotification($id));
  63. }
  64. return $res->toArray();
  65. }
  66. public static function getRankedMaxId($id = false, $start = null, $limit = 10)
  67. {
  68. if(!$start || !$id) {
  69. return [];
  70. }
  71. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [
  72. 'withscores' => true,
  73. 'limit' => [1, $limit]
  74. ]));
  75. }
  76. public static function getRankedMinId($id = false, $end = null, $limit = 10)
  77. {
  78. if(!$end || !$id) {
  79. return [];
  80. }
  81. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [
  82. 'withscores' => true,
  83. 'limit' => [0, $limit]
  84. ]));
  85. }
  86. public static function set($id, $val)
  87. {
  88. return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
  89. }
  90. public static function del($id, $val)
  91. {
  92. Cache::forget('service:notification:' . $val);
  93. return Redis::zrem(self::CACHE_KEY . $id, $val);
  94. }
  95. public static function add($id, $val)
  96. {
  97. return self::set($id, $val);
  98. }
  99. public static function rem($id, $val)
  100. {
  101. return self::del($id, $val);
  102. }
  103. public static function count($id)
  104. {
  105. return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf');
  106. }
  107. public static function getNotification($id)
  108. {
  109. return Cache::remember('service:notification:'.$id, now()->addDays(3), function() use($id) {
  110. $n = Notification::with('item')->findOrFail($id);
  111. $fractal = new Fractal\Manager();
  112. $fractal->setSerializer(new ArraySerializer());
  113. $resource = new Fractal\Resource\Item($n, new NotificationTransformer());
  114. return $fractal->createData($resource)->toArray();
  115. });
  116. }
  117. public static function setNotification(Notification $notification)
  118. {
  119. return Cache::remember('service:notification:'.$notification->id, now()->addDays(3), function() use($notification) {
  120. $fractal = new Fractal\Manager();
  121. $fractal->setSerializer(new ArraySerializer());
  122. $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
  123. return $fractal->createData($resource)->toArray();
  124. });
  125. }
  126. public static function warmCache($id, $stop = 400, $force = false)
  127. {
  128. if(self::count($id) == 0 || $force == true) {
  129. $ids = Notification::whereProfileId($id)
  130. ->latest()
  131. ->limit($stop)
  132. ->pluck('id');
  133. foreach($ids as $key) {
  134. self::set($id, $key);
  135. }
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. }