NotificationService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 set($id, $val)
  43. {
  44. return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
  45. }
  46. public static function del($id, $val)
  47. {
  48. return Redis::zrem(self::CACHE_KEY . $id, $val);
  49. }
  50. public static function add($id, $val)
  51. {
  52. return self::set($id, $val);
  53. }
  54. public static function rem($id, $val)
  55. {
  56. return self::del($id, $val);
  57. }
  58. public static function count($id)
  59. {
  60. return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf');
  61. }
  62. public static function getNotification($id)
  63. {
  64. return Cache::remember('service:notification:'.$id, now()->addMonths(3), function() use($id) {
  65. $n = Notification::with('item')->findOrFail($id);
  66. $fractal = new Fractal\Manager();
  67. $fractal->setSerializer(new ArraySerializer());
  68. $resource = new Fractal\Resource\Item($n, new NotificationTransformer());
  69. return $fractal->createData($resource)->toArray();
  70. });
  71. }
  72. public static function setNotification(Notification $notification)
  73. {
  74. return Cache::remember('service:notification:'.$notification->id, now()->addMonths(3), function() use($notification) {
  75. $fractal = new Fractal\Manager();
  76. $fractal->setSerializer(new ArraySerializer());
  77. $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
  78. return $fractal->createData($resource)->toArray();
  79. });
  80. }
  81. public static function warmCache($id, $stop = 400, $force = false)
  82. {
  83. if(self::count($id) == 0 || $force == true) {
  84. $ids = Notification::whereProfileId($id)
  85. ->latest()
  86. ->limit($stop)
  87. ->pluck('id');
  88. foreach($ids as $key) {
  89. self::set($id, $key);
  90. }
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. }