mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 06:21:27 +00:00
Update BookmarkService, use sorted set
This commit is contained in:
parent
0157566c25
commit
a11772bcfe
3 changed files with 30 additions and 5 deletions
|
@ -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()) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue