coolmatty's avatar

Caching less than 1 minute

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.

0 likes
15 replies
coolmatty's avatar

@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.

JeffreyWay's avatar

Hmm - never had a need to cache anything for less than a minute...

jimmck's avatar

Exactly. Shared memory yes, cache no?

coolmatty's avatar

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.

BrandonSurowiec's avatar

A simple dirty workaround could be to:

  1. Cache the time you built the cache (keep forever)
  2. Cache the queries for a minute

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.

jeliasson's avatar

I would also suggest caching for e.g. 1 minute, and mabye have a cronjob to un-remembering the cache.

pmall's avatar

i wonder what is generating 300+ request per page

bashy's avatar

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.

envision's avatar

Sub-minute caching would be very help here also!

envision's avatar
$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
});
dudod's avatar

Try to use Redis for caching. Something like

Redis::setex('name', 'Taylor', 20);

Where 20 is the caching time in seconds

trevorg's avatar

@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.

https://www.nginx.com/blog/benefits-of-microcaching-nginx/

4 likes
RafaelPazio's avatar

Try:

$time = Cache::remember('key', 0.34, function(){
    return time();
});

I needed to cache for 20 seconds, and the above solution solved my problem!

5 likes

Please or to participate in this conversation.