Update BookmarkService, use sorted set

This commit is contained in:
Daniel Supernault 2022-01-28 18:27:53 -07:00
parent 0157566c25
commit a11772bcfe
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 30 additions and 5 deletions

View file

@ -6,6 +6,7 @@ use App\Bookmark;
use App\Status; use App\Status;
use Auth; use Auth;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Services\BookmarkService;
class BookmarkController extends Controller class BookmarkController extends Controller
{ {
@ -28,7 +29,10 @@ class BookmarkController extends Controller
); );
if (!$bookmark->wasRecentlyCreated) { if (!$bookmark->wasRecentlyCreated) {
BookmarkService::del($profile->id, $status->id);
$bookmark->delete(); $bookmark->delete();
} else {
BookmarkService::add($profile->id, $status->id);
} }
if ($request->ajax()) { if ($request->ajax()) {

View file

@ -43,6 +43,7 @@ use App\Services\SnowflakeService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\UserFilterService; use App\Services\UserFilterService;
use App\Services\DiscoverService; use App\Services\DiscoverService;
use App\Services\BookmarkService;
class InternalApiController extends Controller class InternalApiController extends Controller
{ {
@ -316,12 +317,17 @@ class InternalApiController extends Controller
public function bookmarks(Request $request) public function bookmarks(Request $request)
{ {
$res = Bookmark::whereProfileId($request->user()->profile_id) $pid = $request->user()->profile_id;
$res = Bookmark::whereProfileId($pid)
->orderByDesc('created_at') ->orderByDesc('created_at')
->simplePaginate(10) ->simplePaginate(10)
->map(function($bookmark) { ->map(function($bookmark) use($pid) {
$status = StatusService::get($bookmark->status_id, false); $status = StatusService::get($bookmark->status_id, false);
$status['bookmarked_at'] = $bookmark->created_at->format('c'); $status['bookmarked_at'] = $bookmark->created_at->format('c');
if($status) {
BookmarkService::add($pid, $status['id']);
}
return $status; return $status;
}) })
->filter(function($bookmark) { ->filter(function($bookmark) {

View file

@ -4,13 +4,28 @@ namespace App\Services;
use App\Bookmark; use App\Bookmark;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class BookmarkService class BookmarkService
{ {
const CACHE_KEY = 'pf:services:bookmarks:';
public static function get($profileId, $statusId) public static function get($profileId, $statusId)
{ {
// return Cache::remember('pf:bookmarks:' . $profileId . ':' . $statusId, 84600, function() use($profileId, $statusId) { if (!Redis::zcard(self::CACHE_KEY . $profileId)) {
return Bookmark::whereProfileId($profileId)->whereStatusId($statusId)->exists(); 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);
} }
} }