diff --git a/app/Http/Controllers/BookmarkController.php b/app/Http/Controllers/BookmarkController.php index 0357a36dd..a340460b9 100644 --- a/app/Http/Controllers/BookmarkController.php +++ b/app/Http/Controllers/BookmarkController.php @@ -6,6 +6,7 @@ use App\Bookmark; use App\Status; use Auth; use Illuminate\Http\Request; +use App\Services\BookmarkService; class BookmarkController extends Controller { @@ -28,7 +29,10 @@ class BookmarkController extends Controller ); if (!$bookmark->wasRecentlyCreated) { + BookmarkService::del($profile->id, $status->id); $bookmark->delete(); + } else { + BookmarkService::add($profile->id, $status->id); } if ($request->ajax()) { diff --git a/app/Http/Controllers/InternalApiController.php b/app/Http/Controllers/InternalApiController.php index cf670b21b..4391c3c3c 100644 --- a/app/Http/Controllers/InternalApiController.php +++ b/app/Http/Controllers/InternalApiController.php @@ -43,6 +43,7 @@ use App\Services\SnowflakeService; use App\Services\StatusService; use App\Services\UserFilterService; use App\Services\DiscoverService; +use App\Services\BookmarkService; class InternalApiController extends Controller { @@ -316,12 +317,17 @@ class InternalApiController extends Controller public function bookmarks(Request $request) { - $res = Bookmark::whereProfileId($request->user()->profile_id) + $pid = $request->user()->profile_id; + $res = Bookmark::whereProfileId($pid) ->orderByDesc('created_at') ->simplePaginate(10) - ->map(function($bookmark) { + ->map(function($bookmark) use($pid) { $status = StatusService::get($bookmark->status_id, false); $status['bookmarked_at'] = $bookmark->created_at->format('c'); + + if($status) { + BookmarkService::add($pid, $status['id']); + } return $status; }) ->filter(function($bookmark) { diff --git a/app/Services/BookmarkService.php b/app/Services/BookmarkService.php index 0e2b8465e..5af9625aa 100644 --- a/app/Services/BookmarkService.php +++ b/app/Services/BookmarkService.php @@ -4,13 +4,28 @@ namespace App\Services; use App\Bookmark; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Redis; class BookmarkService { + const CACHE_KEY = 'pf:services:bookmarks:'; + public static function get($profileId, $statusId) { - // return Cache::remember('pf:bookmarks:' . $profileId . ':' . $statusId, 84600, function() use($profileId, $statusId) { - return Bookmark::whereProfileId($profileId)->whereStatusId($statusId)->exists(); - // }); + if (!Redis::zcard(self::CACHE_KEY . $profileId)) { + return false; + } + + return Redis::zscore(self::CACHE_KEY . $profileId, $statusId) != null; + } + + public static function add($profileId, $statusId) + { + return Redis::zadd(self::CACHE_KEY . $profileId, $statusId, $statusId); + } + + public static function del($profileId, $statusId) + { + return Redis::zrem(self::CACHE_KEY . $profileId, $statusId); } }