put
takes key
, data
and minutes
, not an expiry time. If you want to add an expiry time you'll have to extend it to support that.
https://github.com/laravel/framework/blob/master/src/Illuminate/Cache/StoreInterface.php#L13
I looked at the documentation on Laravel's site for cache and used the example.
Using Carbon Objects To Set Expire Time
$expiresAt = Carbon::now()->addMinutes(10);
Cache::put('key', 'value', $expiresAt);
Guess I'm confused. The documentation says it's ok to pass a Carbon Object.
I looked through the code and it doesn't check for a Carbon Object, it just looks at it in terms of minutes. :/
Looks like documentation error since it doesn't work with Carbon.
After a quick test, even though it says the parameter is an INT, I passed in a decimal and the cache works for under 1 minute.
Sorry, it's not a documentation error it was mine. The cache class isn't used directly, it's used through the repository
class that adds the support for Carbon, as seen here: https://github.com/illuminate/cache/blob/master/Repository.php#L236 my bad! Not sure why it isn't working for you.
Oh interesting. Ya, doesn't work when I pass the Carbon Object. But it does work when passing a decimal.
Thanks for the help. It's working now, just slightly different than how I wanted. If I have time I'll dig into why it's not working with Carbon.
nothing fancy, as you add seconds to DateTime, the "getMinutes" method in Repo return a zero value ...
$duration = Carbon::now()->addSeconds(20);
return max( 0, Carbon::now()->diffInMinutes( $duration, false ) ); // return 0
When this value is stored, a zero value means forever.
the decimal approach seems the only way to store in seconds or as mentioned by @citricsquid, extend the repo and the cache driver.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community