May 12, 2023
0
Level 13
Cache data until the end of current season
Using laravel remember method how do you cache data until the end of current season?
For example the spring ends in June 21 so it should get the current date and add necessary days to it:
$seasons = [
'spring' => ['month' => 3, 'day' => 20],
'summer' => ['month' => 6, 'day' => 21],
'fall' => ['month' => 9, 'day' => 22],
'winter' => ['month' => 12, 'day' => 21],
];
$currentDate = Carbon::now();
$currentSeason = null;
foreach ($seasons as $season => $startDateInfo) {
$startDate = Carbon::create(null, $startDateInfo['month'], $startDateInfo['day']);
if ($currentDate->lessThan($startDate)) {
break;
}
$currentSeason = $season;
}
if (!$currentSeason) {
$currentSeason = 'spring';
$currentDate->addYear()->startOfYear();
}
$endDate = Carbon::create(null, $seasons[$currentSeason]['month'] + 2, $seasons[$currentSeason]['day'])->subDay();
$cacheExpiration = Carbon::now()->diffInSeconds($endDate, false);
$data = Cache::remember('my_data', $cacheExpiration, function () {
return getDataAndProcess();
});
This is the ChatGPT solution and is not correct
Please or to participate in this conversation.