CollectionService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Services;
  3. use App\Collection;
  4. use App\CollectionItem;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Redis;
  7. class CollectionService
  8. {
  9. const CACHE_KEY = 'pf:services:collections:';
  10. public static function getItems($id, $start = 0, $stop = 10)
  11. {
  12. if(self::count($id)) {
  13. return Redis::zrangebyscore(self::CACHE_KEY . 'items:' . $id, $start, $stop);
  14. }
  15. return self::coldBootItems($id);
  16. }
  17. public static function addItem($id, $sid, $score)
  18. {
  19. Redis::zadd(self::CACHE_KEY . 'items:' . $id, $score, $sid);
  20. }
  21. public static function removeItem($id, $sid)
  22. {
  23. Redis::zrem(self::CACHE_KEY . 'items:' . $id, $sid);
  24. }
  25. public static function clearItems($id)
  26. {
  27. Redis::del(self::CACHE_KEY . 'items:' . $id);
  28. }
  29. public static function coldBootItems($id)
  30. {
  31. return Cache::remember(self::CACHE_KEY . 'items:' . $id, 86400, function() use($id) {
  32. return CollectionItem::whereCollectionId($id)
  33. ->orderBy('order')
  34. ->get()
  35. ->each(function($item) use ($id) {
  36. self::addItem($id, $item->object_id, $item->order);
  37. })
  38. ->map(function($item) {
  39. return (string) $item->object_id;
  40. })
  41. ->values()
  42. ->toArray();
  43. });
  44. }
  45. public static function count($id)
  46. {
  47. $count = Redis::zcard(self::CACHE_KEY . 'items:' . $id);
  48. if(!$count) {
  49. self::coldBootItems($id);
  50. $count = Redis::zcard(self::CACHE_KEY . 'items:' . $id);
  51. }
  52. return $count;
  53. }
  54. public static function getCollection($id)
  55. {
  56. $collection = Cache::remember(self::CACHE_KEY . 'get:' . $id, 86400, function() use($id) {
  57. $collection = Collection::find($id);
  58. if(!$collection) {
  59. return false;
  60. }
  61. $account = AccountService::get($collection->profile_id);
  62. if(!$account) {
  63. return false;
  64. }
  65. return [
  66. 'id' => (string) $collection->id,
  67. 'pid' => (string) $collection->profile_id,
  68. 'username' => $account['username'],
  69. 'visibility' => $collection->visibility,
  70. 'title' => $collection->title,
  71. 'description' => $collection->description,
  72. 'thumb' => self::getThumb($id),
  73. 'url' => $collection->url(),
  74. 'published_at' => $collection->published_at
  75. ];
  76. });
  77. if($collection) {
  78. $collection['post_count'] = self::count($id);
  79. }
  80. return $collection;
  81. }
  82. public static function setCollection($id, $collection)
  83. {
  84. $account = AccountService::get($collection->profile_id);
  85. if(!$account) {
  86. return false;
  87. }
  88. $res = [
  89. 'id' => (string) $collection->id,
  90. 'pid' => (string) $collection->profile_id,
  91. 'username' => $account['username'],
  92. 'visibility' => $collection->visibility,
  93. 'title' => $collection->title,
  94. 'description' => $collection->description,
  95. 'thumb' => self::getThumb($id),
  96. 'url' => $collection->url(),
  97. 'published_at' => $collection->published_at
  98. ];
  99. Cache::put(self::CACHE_KEY . 'get:' . $id, $res, 86400);
  100. $res['post_count'] = self::count($id);
  101. return $res;
  102. }
  103. public static function deleteCollection($id)
  104. {
  105. Redis::del(self::CACHE_KEY . 'items:' . $id);
  106. Cache::forget(self::CACHE_KEY . 'get:' . $id);
  107. }
  108. public static function getThumb($id)
  109. {
  110. $item = self::getItems($id, 0, 1);
  111. if(!$item || empty($item)) {
  112. return '/storage/no-preview.png';
  113. }
  114. $status = StatusService::get($item[0]);
  115. if(!$status) {
  116. return '/storage/no-preview.png';
  117. }
  118. if(!isset($status['media_attachments']) || empty($status['media_attachments'])) {
  119. return '/storage/no-preview.png';
  120. }
  121. return $status['media_attachments'][0]['url'];
  122. }
  123. }