PHP laravel 过去多少天后再进行操作

use Illuminate\Support\Facades\Cache;

function queryCurrencyService(): JsonResponse
{
    $res = CurrencyName::query()
        ->select(['name', 'code'])
        ->get()
        ->toArray();

    // 获取上次拉取的时间
    $lastFetchedAt = Cache::get('last_fetched_at');

    $daysSinceLastFetch = $lastFetchedAt ? now()->diffInDays($lastFetchedAt) : null;

    // 检查是否需要拉取
    $shouldFetch = (!$res) || (!$lastFetchedAt) || ($daysSinceLastFetch && $daysSinceLastFetch > 3);

    if ($shouldFetch) {
        // 拉取数据的代码...

        // 更新上次拉取的时间,并缓存3天
        Cache::put('last_fetched_at', now(), 3 * 24 * 60); 
    }

    return response()->json($res);
}

你可能感兴趣的:(php,laravel,android)