PublicTimelineService.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Redis;
  4. use App\{
  5. Profile,
  6. Status,
  7. UserFilter
  8. };
  9. class PublicTimelineService {
  10. const CACHE_KEY = 'pf:services:timeline:public';
  11. public static function get($start = 0, $stop = 10)
  12. {
  13. if($stop > 100) {
  14. $stop = 100;
  15. }
  16. $tl = [];
  17. $keys = Redis::zrevrange(self::CACHE_KEY, $start, $stop);
  18. foreach($keys as $key) {
  19. array_push($tl, StatusService::get($key));
  20. }
  21. return $tl;
  22. }
  23. public static function add($val)
  24. {
  25. return Redis::zadd(self::CACHE_KEY, 1, $val);
  26. }
  27. public static function rem($val)
  28. {
  29. return Redis::zrem(self::CACHE_KEY, $val);
  30. }
  31. public static function del($val)
  32. {
  33. return self::rem($val);
  34. }
  35. public static function count()
  36. {
  37. return Redis::zcount(self::CACHE_KEY, '-inf', '+inf');
  38. }
  39. public static function warmCache($force = false, $limit = 100)
  40. {
  41. if(self::count() == 0 || $force == true) {
  42. $ids = Status::whereNull('uri')
  43. ->whereNull('in_reply_to_id')
  44. ->whereNull('reblog_of_id')
  45. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  46. ->whereScope('public')
  47. ->latest()
  48. ->limit($limit)
  49. ->pluck('id');
  50. foreach($ids as $id) {
  51. self::add($id);
  52. }
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. }