refactor(Core): sent cache
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\RecentArt;
|
||||
use App\Models\UserSubscriber;
|
||||
use App\Services\AppMarketPurchaseVerifier;
|
||||
use App\Traits\BaseApiResponse;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
@@ -22,7 +23,7 @@ class HomeController extends Controller
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$this->refreshMarketSubscription($user);
|
||||
$this->refreshMarketSubscriptionAfterResponse($user);
|
||||
|
||||
$recent = RecentArt::query()->where('user_id', $user->id)->get()->map(function ($q) {
|
||||
return [
|
||||
@@ -115,6 +116,20 @@ class HomeController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
private function refreshMarketSubscriptionAfterResponse($user): void
|
||||
{
|
||||
$cacheKey = "home-market-refresh-user-{$user->id}";
|
||||
$this->refreshMarketSubscription($user);
|
||||
|
||||
if (!Cache::add($cacheKey, true, now()->addHours(24))) {
|
||||
return;
|
||||
}
|
||||
|
||||
app()->terminating(function () use ($user) {
|
||||
$this->refreshMarketSubscription($user);
|
||||
});
|
||||
}
|
||||
|
||||
private function refreshMarketSubscription($user): void
|
||||
{
|
||||
$bazaarSubscription = $user->userSubscribers()
|
||||
@@ -127,7 +142,7 @@ class HomeController extends Controller
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if ($bazaarSubscription && $this->marketVerifier->refreshBazaarSubscriber($bazaarSubscription)) {
|
||||
if ($bazaarSubscription && $this->shouldRefreshMarketSubscription($bazaarSubscription) && $this->marketVerifier->refreshBazaarSubscriber($bazaarSubscription)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -138,8 +153,17 @@ class HomeController extends Controller
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if ($myketSubscription) {
|
||||
if ($myketSubscription && $this->shouldRefreshMarketSubscription($myketSubscription)) {
|
||||
$this->marketVerifier->refreshMyketSubscriber($myketSubscription);
|
||||
}
|
||||
}
|
||||
|
||||
private function shouldRefreshMarketSubscription(UserSubscriber $subscriber): bool
|
||||
{
|
||||
if ($subscriber->expired_at && $subscriber->expired_at->lessThanOrEqualTo(now())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !$subscriber->last_verified_at || $subscriber->last_verified_at->lessThan(now()->subHours(12));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user