Laravelvideo· May 9, 2025· 14:41· 14.9K views on YouTube
React at a moment you like. The best moments light up here.
All right, I want to talk about what this word means when it comes to Laravel. So, what is a cache? Well, the dictionary says that it's something that is either stored or hidden. Now, what does that mean within computer language? Well, basically, when we're talking about caching, we're saying that something is stored in a place that is easily retrieved. So that way uh when
you need it, it can take a lot faster to get you that information than requesting it from a farther away place. You have several options when it comes to cache in Laravel. Okay, where are you going to store that cash? You have things like Reddus or Dynamo DB or memach for example. But really the question becomes how should you use a cache or maybe when
should you use a cache? What are some things that you have to think about when using a cache or just what are some best practices around caching within Laravel? Well, let's take a look. Now, the first thing to note when it comes to caching, especially if you are new to caching, is that it's really easy to fall into the trap of just putting everything that you have in a cache. If you're retrieving data from the database and it doesn't change frequently, might as well just
put into a cache, right? Not necessarily. Sometimes it's best to examine when do you actually need a cache. In this case, when do you need a way to retrieve information that might not otherwise change and then evaluate if that should be in a cache or not. And while I can't tell you specifically for your application when you should start caching specific data queries or
requests, it's great to default to not caching something until you absolutely need it. And things like Laravel Nightwatch, which is an upcoming product by the Laravel team, are going to be great to let you know, okay, maybe this should be something that is cached because it is taking quite a long time for something that doesn't change frequently. Now, first off, where can you store this cash? aka the information that you need to retrieve faster than
you otherwise would need to retrieve it if you're just requesting it from the database. Well, two typical standard options would be memcached or reddus which are two very fast in-memory database options specifically for caching. But of course, through Laravel's configuration, you also have the option of using something like Dynamo DB or even your relational database as a cache driver. Something stored in the cache needs to have a key and a value. It needs to be unique
across all of the things stored in your cache. And then the cache facade or even the cache helper allow you to get things based off that particular value. They allow you to store things based on a particular value as well as check if there is something in the cache with that particular value. Something like cache has. But instead of having to check if the cache has a particular
value in the cache and then grabbing that value and pushing it to your view file or setting it if there is no value, there's an easier way to do all of that in once and that's going to be the cache remember option to say okay if it's not there then store if it is there then get it. And you also can pass in particular seconds to say okay well I want to store it for this amount of time and if that
amount of time has passed then I want to get a new cache value. So keep in mind this is a great way to keep all of that syntax as short and as concise as possible. So that way instead of having to write those if statements and set the value or store it if it's not there, you can just use remember. I found that this is one that I reach for quite a bit when
I'm remembering something in the cache based off of a specific time frame. What does this look like put in practice? Well, every 10 seconds, we're going to get the new server time and add it to the cache in this particular server time cache value. But this just means anyone who is requesting our site is going to have that same cacheed server time until 10 seconds has been complete. All right,
31 4631. Refresh. Refresh. We have to wait 10 seconds. This is not going to update until 10 seconds has passed. So refresh. And now it updates. So when might you use something like remember? Well, remember that caching is best used for values that don't change frequently. So, one thing might be something in your
admin dashboard where your total users might not change every 10 seconds, maybe every hour, but you usually want to grab a whole list of them to display in your admin dashboard or maybe even just in some analytics tab somewhere on your site. In that case, that'd be great if you didn't want to have to display all that information. Instead, just use one expensive query every couple of hours.
Speaking about remember, I hope you remember to like and subscribe, especially if you're a web artisan who just likes to ship and wants to see more content about learning how to ship faster. Now, you might have heard of this relatively newer method within the cash facade. It's cash flexible, and this is a great alternative to remember. In fact, for the most part, you might choose to use this over most of the remember options, particularly for your
application. If a request is made within the fresh period before the first value, this case 5 seconds, then the cache is returned immediately. But if it's made during the stale period, aka 6 seconds, 7 seconds, 8 seconds, 9 seconds, then the stale value, the one that was served within these first 5 seconds is still served to the user until that deferred
function tries to retrieve a new cached value and then it just replaces itself. The goal of this is that your users then hopefully don't have to wait as long for that new information. What does this look like? Switching this remember to then flexible. Well, instead of 10 seconds, let's do 5 seconds and 10 seconds to say, okay, I want to serve the cached information for 5 seconds.
But then after 5 seconds, let's get some new information in the background. Even while requests are still going between 5 seconds and 10 seconds, my users are still going to see that stale information, but hopefully eventually they will receive that new information that was now cached in the background. So, what does that look like? Okay, 5221. We refresh 47. Refresh. Refresh. Refresh. We shouldn't see that 2-cond
load in the background. And it just keeps going. You don't see any kind of loading indicator even though we're sleeping for one second. Wonder when we even sleep for 5 seconds. And we'll refresh this. Refresh. Refresh. Okay, that was a little bit of a 5-second load right there. But there we go. Refresh. Refresh. Refresh. Refresh. Refresh.
Refresh. And you saw that time changed. I said refresh a lot, but we didn't have to wait 5 seconds again once that initial data was stored. That flexible allowed you to defer that retrieval of this information. So where is something like flexible useful for? This I would say would be great for anything that is userfacing. Maybe it's particular uh
queries that most of your users have within their own particular dashboard. Or maybe it's something like comments for posts that don't necessarily need to be as real time as other things within your application. But it's nice to be able to have them uh be fast, be flexible, and being able to have all that information given to your users without having to have your users wait for that information to be pushed to the
view is incredible. I also think that the cache flexible option is a perfect pairing with something like the inertia defer method to be able to say, "Okay, I'm already deferring this to the front end so that my users don't have to wait for that information that's being loaded." Well, now if your server is also fetching that information behind the scenes, not only do you have up-to-date information that is quick
because it's cache, but the times that your users are having to wait for that fresh information in between that stale while validate period, well, it's deferred anyways. So, they're not really having to wait too long at all. Now, there is the ability to retrieve a specific value from the cache and then immediately delete it once it's retrieved. When would you use something like this? Of course, this is all subjective and depending on your
specific application. Most of the time, you probably just want to reach for something like local storage where you need to store information temporarily and then you might need to retrieve it. But for things that need to be saved a little bit more permanently, that one-time cache save and then retrieve using pull might be perfect. One example might be if you have initial setup data
for your application that you need all that information stored somewhere but you don't necessarily want to have uh it being stored in the database and it can easily be replicated or easily be recreated but it does need to be fast in order for that user to keep going through that maybe setup or onboarding process. Again, all subjective, but this is how you need to think through your application when it comes to caching.
You don't just want to put everything right behind the cache just because it makes things faster. It does, but it also adds complexity if you don't necessarily need it. So, think through it first. It needs to be intentional. And when it comes to being intentional, sometimes the best form of intentionality is not actually doing it at all. Maybe the only caching you need is the caching that Laravel gives you out of the box with things like route caching as well as view caching to be
able to say, okay, when you are in production, those blade views that you might have or all those routes that you have, um, you just want to decrease the time it takes for your users to actually serve those up or be served up. And Laravel gives you the option of caching those routes and caching those views until information changes. Which brings me to the final best practice tip. You need to know and be intentional about
when you're caching those particular values in your application and even when it comes to routes and views from your production standpoint. But you also need to know when you need to break your cache or flush your cache as well. What do I mean by this? Well, if you added a new route and your routes are being cached in production, you need to make sure that you clear that route cache so that that new route can be served up.
You need to be intentional about what you're storing or setting in the cache. And even if you need to set it, if this if this query or information that you're usually retrieving takes a long time, two, three, four plus seconds, then yes, it's a good option. but is it being stored the proper amount of time and are you breaking, flushing, forgetting that cache when you actually need to? So be thinking about this before you implement
it so that your users aren't strug struggling to find the right information because they don't have that up-to-date information. So things like remember or things like flexible give you that option of of having those caches but also not having to remember to break them when you need it. But when you do need to have a more manual approach, this cash forget, passing in that key is incredibly helpful. Or if you just want
to flush the entire cache, cache flush will do that for you. Speaking of, what does that cache look like in your database? And this is my Reddus cache that I set up to do that server time. Well, it's simply storing it with the particular key and then that value that we set. All of this is just information in a in-memory database like Reddus or if you're using the database option or
something like memcache. So it's really easy to also view that just as you would with any standard database viewer. So when it comes to caching and and thinking about best practices for using the cache options within your application, I think it's best to take a uh do I need it approach and if I do, what kind of cache do I actually need? Maybe it's as simple as just caching it
broadly from your CDN or from your uh deployment platform like some using something like Cloudflare which Laraveo Cloud for example uses to cache your assets or just taking it one step lower. Are you caching your routes and your views properly? are are the things that you need to show your users that don't change or maybe they only change, you know, when you actually add a new route
or a new view. Are are those being cached properly? And then lastly, maybe you do need to cache those data requests that are taking a significant amount of time for your users and slowing things down in the process. And maybe you only need to cache it for an hour, maybe maybe 10 minutes, maybe even just 30 seconds will help in the long run. So, yes, caching is awesome, and it's it's really neat to know that you can just
throw it behind a cache and say, "Hey, this is now going to be served just a little bit faster to my users." But you need to be thinking, is this something that you actually need to be caching? And are you prepared to handle the complexities that come with that of am I caching the right amount of information? Am I caching it for the right amount of time? And then, am I serving it to the right amount of people? So yeah, caching is really just about choices.