Cache TTL Now Seconds0:00
Next up, we have a small but very important change to the cache API. To bring it more in line with PSR-16, which is a cache library standard, you will now declare your timeouts, or your TTL, in seconds rather than minutes. So as an example, in Laravel 5.7 and below, to cache the key foo for an hour, you'd specify 60 minutes. But now, you'd have to convert that to seconds. So as an illustration, 60 seconds in one minute, and there are 60 minutes in an hour. So that would then become 3600. So if you are upgrading your application, find any place where you reference cache put, or remember, or putMany, any of those, and then make sure you update it to seconds.
Upgrading Existing Cache Calls0:36
So if you are upgrading your application, find any place where you reference cache put, or remember, or putMany, any of those, and then make sure you update it to seconds. So I'll show you a quick example. Here's an AchievementServiceProvider that I have for Laracast. This is where I declare and register all available achievements for Laracast. Anyways, you can see that we collect this big array here, and for each one, we instantiate it, and there's also a bit of syncing with the database that takes place. So to improve performance, we cache it, and then, at least for this example, we cache it for a total of 1440. And that would be a day.
Using Date Helpers for TTL1:38
24 hours in a day. So 86,400. Now this can even be more confusing when you come back to it. So if you want, Laravel includes a now helper that will give you a fresh DateTime instance for the current time. So what you could do is say now(), and then add seconds. Or add minutes, or add a day, or days. So when we take this approach behind the scenes, Laravel is going to convert it to seconds. So that's often a good way to go. And in fact, if you want to take a look, let's look for the remember method.
Inspecting TTL Conversion Internals2:09
So that's often a good way to go. And in fact, if you want to take a look, let's look for the remember method. Alright, here's the TTL. That's going to defer to the put method. And if we scroll down, we convert that to seconds. There we go. Get the diff in real seconds. But anyways, once again, the key thing to remember. If upgrading your Laravel application, find any place where you are using cache::put or cache::remember, or any of those, and ensure that you convert it from minutes to seconds.
If upgrading your Laravel application, find any place where you are using cache::put or cache::remember, or any of those, and ensure that you convert it from minutes to seconds.
