Laravel 4 : cache that does not expire

Cache that does not expire

Posted By: pmietlicki

Published to Laravel on Jun 23, 2017

// want to cache the data with an expiry date. 
// I do not want the cache to expire on that date, I want to make an attempt to re-request the data and if successful, overwrite the old data.
// IF the re-request is not successful, it should leave the old data alone.
Cache::macro('untilUpdated', function ($key, $date, Closure $callback)
{
if ( ! $date instanceof DateTime)
{
$date = Carbon\Carbon::now()->addMinutes($date);
}
if (is_null($value = Cache::get($key)))
{
$data = $callback();
Cache::forever($key, compact('date', 'data'));
return $data;
}
if (!isSet($value['date']))
return $value;
if ($value['date'] >= $date && ($data = $callback()) !== false)
{
Cache::forever($key, compact('date', 'data'));
return $data;
}
return $value['data'];
});


Tags: laravel, php

Archive