Carbon has an addSeconds() method.
Carbon::now()->addSeconds(30); // 30 seconds
For some reason, it appears that Laravel does not support caching anything for less than 1 minute. After looking deep into the code, it assumes minutes even if you pass a Carbon object (it uses the minutes method).
Is there any way to work around this to actually cache for less than a minute? It'd be annoying to write my own caching extension to just add the ability to do seconds.
Carbon has an addSeconds() method.
Carbon::now()->addSeconds(30); // 30 seconds
@Niban Yes, but the Cache::Remember (and put) does not make use of seconds when it reads the carbon object you provide. It pulls the difference in minutes from the Carbon object you provide.
Hmm - never had a need to cache anything for less than a minute...
Exactly. Shared memory yes, cache no?
That's unfortunate, because it'd be very useful right now. I have a few pages that run very very complex queries (at least, when you have potentially 300+ requests per second for the page), and the data is expected not to be stale. I was hoping to cache it even for just 10-30 seconds, which would take a dramatic amount of load off the server.
I think this is something Taylor should fix in the documentation at the very least, as it is extremely non-obvious that passing a Carbon object does not result in exact cache expiration. It honestly should, as I can't see any reason why NOT to allow seconds for caching on a Carbon object. The data's already there, and there's no performance impact whatsoever.
A simple dirty workaround could be to:
Then grab #1, if it is greater than 30 seconds, rebuild the query cache. You could even code yourself a little helper function to implement the above. Hopefully it makes sense.
I would also suggest caching for e.g. 1 minute, and mabye have a cronjob to un-remembering the cache.
i wonder what is generating 300+ request per page
Why not extend the cache code and query the time yourself? Then you can do a calculation on if the time is greater than X seconds.
Sub-minute caching would be very help here also!
$forgetSeconds = 10;
$lastRequestTs = Cache::get("state-requested-at-for-some-$id");
if (is_integer($lastRequestTs) && time() - $lastRequestTs > $forgetSeconds)
Cache::forget("state-requested-at-for-some-$id");
Cache::forever("state-requested-at-for-some-$id", time());
$cacheMinutes = 1;
return $state = Cache::remember("state-for-some-$id", $cacheMinutes, function() use ($id)
{
// work your magic
});
Try to use Redis for caching. Something like
Redis::setex('name', 'Taylor', 20);
Where 20 is the caching time in seconds
@JeffreyWay I would definitely recommend Laravel consider allowing for caching for any amount of time. There are certainly scenarios where Laravel is used to power apps that accept hundreds of requests/sec. A 5 second cache would yield considerable performance improvements without sacrificing much in terms of live data presentation.
Microcaching is a caching technique whereby content is cached for a very short period of time, perhaps as little as 1 second.
Try:
$time = Cache::remember('key', 0.34, function(){
return time();
});
I needed to cache for 20 seconds, and the above solution solved my problem!
@RafaelPazio yes. Laravel 5.3 allows float ttl values.
Please or to participate in this conversation.