NotificationService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. $n = self::getNotification($id);
  26. if($n != null) {
  27. $res->push($n);
  28. }
  29. }
  30. return $res;
  31. }
  32. public static function coldGet($id, $start = 0, $stop = 400)
  33. {
  34. $stop = $stop > 400 ? 400 : $stop;
  35. $ids = Notification::whereProfileId($id)
  36. ->latest()
  37. ->skip($start)
  38. ->take($stop)
  39. ->pluck('id');
  40. foreach($ids as $key) {
  41. self::set($id, $key);
  42. }
  43. return $ids;
  44. }
  45. public static function getMax($id = false, $start = 0, $limit = 10)
  46. {
  47. $ids = self::getRankedMaxId($id, $start, $limit);
  48. if(empty($ids)) {
  49. return [];
  50. }
  51. $res = collect([]);
  52. foreach($ids as $id) {
  53. $n = self::getNotification($id);
  54. if($n != null) {
  55. $res->push($n);
  56. }
  57. }
  58. return $res->toArray();
  59. }
  60. public static function getMin($id = false, $start = 0, $limit = 10)
  61. {
  62. $ids = self::getRankedMinId($id, $start, $limit);
  63. if(empty($ids)) {
  64. return [];
  65. }
  66. $res = collect([]);
  67. foreach($ids as $id) {
  68. $n = self::getNotification($id);
  69. if($n != null) {
  70. $res->push($n);
  71. }
  72. }
  73. return $res->toArray();
  74. }
  75. public static function getRankedMaxId($id = false, $start = null, $limit = 10)
  76. {
  77. if(!$start || !$id) {
  78. return [];
  79. }
  80. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [
  81. 'withscores' => true,
  82. 'limit' => [1, $limit]
  83. ]));
  84. }
  85. public static function getRankedMinId($id = false, $end = null, $limit = 10)
  86. {
  87. if(!$end || !$id) {
  88. return [];
  89. }
  90. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [
  91. 'withscores' => true,
  92. 'limit' => [0, $limit]
  93. ]));
  94. }
  95. public static function set($id, $val)
  96. {
  97. return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
  98. }
  99. public static function del($id, $val)
  100. {
  101. Cache::forget('service:notification:' . $val);
  102. return Redis::zrem(self::CACHE_KEY . $id, $val);
  103. }
  104. public static function add($id, $val)
  105. {
  106. return self::set($id, $val);
  107. }
  108. public static function rem($id, $val)
  109. {
  110. return self::del($id, $val);
  111. }
  112. public static function count($id)
  113. {
  114. return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf');
  115. }
  116. public static function getNotification($id)
  117. {
  118. return Cache::remember('service:notification:'.$id, now()->addDays(3), function() use($id) {
  119. $n = Notification::with('item')->find($id);
  120. if(!$n) {
  121. return null;
  122. }
  123. $fractal = new Fractal\Manager();
  124. $fractal->setSerializer(new ArraySerializer());
  125. $resource = new Fractal\Resource\Item($n, new NotificationTransformer());
  126. return $fractal->createData($resource)->toArray();
  127. });
  128. }
  129. public static function setNotification(Notification $notification)
  130. {
  131. return Cache::remember('service:notification:'.$notification->id, now()->addDays(3), function() use($notification) {
  132. $fractal = new Fractal\Manager();
  133. $fractal->setSerializer(new ArraySerializer());
  134. $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
  135. return $fractal->createData($resource)->toArray();
  136. });
  137. }
  138. public static function warmCache($id, $stop = 400, $force = false)
  139. {
  140. if(self::count($id) == 0 || $force == true) {
  141. $ids = Notification::whereProfileId($id)
  142. ->latest()
  143. ->limit($stop)
  144. ->pluck('id');
  145. foreach($ids as $key) {
  146. self::set($id, $key);
  147. }
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. }