ReblogService.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\Status;
  6. class ReblogService
  7. {
  8. const CACHE_KEY = 'pf:services:reblogs:';
  9. const REBLOGS_KEY = 'pf:services:reblogs:v1:post:';
  10. const COLDBOOT_KEY = 'pf:services:reblogs:v1:post_:';
  11. public static function get($profileId, $statusId)
  12. {
  13. if (!Redis::zcard(self::CACHE_KEY . $profileId)) {
  14. return false;
  15. }
  16. return Redis::zscore(self::CACHE_KEY . $profileId, $statusId) != null;
  17. }
  18. public static function add($profileId, $statusId)
  19. {
  20. return Redis::zadd(self::CACHE_KEY . $profileId, $statusId, $statusId);
  21. }
  22. public static function del($profileId, $statusId)
  23. {
  24. return Redis::zrem(self::CACHE_KEY . $profileId, $statusId);
  25. }
  26. public static function getPostReblogs($id, $start = 0, $stop = 10)
  27. {
  28. if(!Redis::zcard(self::REBLOGS_KEY . $id)) {
  29. return Cache::remember(self::COLDBOOT_KEY . $id, 86400, function() use($id) {
  30. return Status::whereReblogOfId($id)
  31. ->pluck('id')
  32. ->each(function($reblog) use($id) {
  33. self::addPostReblog($id, $reblog);
  34. })
  35. ->map(function($reblog) {
  36. return (string) $reblog;
  37. });
  38. });
  39. }
  40. return Redis::zrange(self::REBLOGS_KEY . $id, $start, $stop);
  41. }
  42. public static function addPostReblog($parentId, $reblogId)
  43. {
  44. $pid = intval($parentId);
  45. $id = intval($reblogId);
  46. if($pid && $id) {
  47. return Redis::zadd(self::REBLOGS_KEY . $pid, $id, $id);
  48. }
  49. }
  50. public static function removePostReblog($parentId, $reblogId)
  51. {
  52. $pid = intval($parentId);
  53. $id = intval($reblogId);
  54. if($pid && $id) {
  55. return Redis::zrem(self::REBLOGS_KEY . $pid, $id);
  56. }
  57. }
  58. }