Playing
02. Flexible cache method

Transcript

00:00
The new cache flexible method in Laravel uses the stale while revalidate pattern to define a stale period of cache data that is deferred to the background to revalidate if it sits in between the fresh and the stale period. Now if you're using caching in your applications, this can
00:18
help speed up requests for data you're happy to compromise freshness over speed. Let's go ahead and take a look. So if you're not already using caching, let's just take a look at a really basic example of caching a bunch of data. So we'll go ahead and use the cache helper here and we're just going to say remember. Let's go ahead and just give this a key so we'll just say users. Let's
00:39
give this a seconds expiry so I'm just going to keep this low very purposely for this episode and we'll go ahead and define out a closure which returns a bunch of data. So I'm just going to grab all of the users that are in the database. Over in the database I've already gone ahead and seeded 5,000 users so we've got plenty of data to check speeds and whether we have any queries or not.
01:01
Okay let's head back over here and let's go ahead and assign this a value and let's die dump on users. So if you are new to caching Laravel what's going to happen here is this closure is going to be invoked. It will return all of these users immediately but inside of this process it will go ahead and cache this in whichever cache driver you are using and the next time this request comes
01:24
around it will serve the cache version. If 10 seconds has passed since this was cached it will be revalidated and this closure will run again to fetch any fresh data. Before we run this let's just go over and just check which cache driver we're using or cache store we're using and at the moment we're using the database. I'm just going to switch this over to file just for the purpose of this.
01:47
Okay so let's go ahead and just make sure we pull in the user model in here and let's go over and run this. Okay so we get back these users the next time I run this that is now pulling from our cache in our case our file store. So what we're going to do is just very quickly install Laravel debug bar just so we can see that the query does actually disappear. So let's pull this in and
02:09
let's go ahead and return a view here just so we can get the benefit of that debug bar. So I'm just going to go ahead and make out a really simple home view here and we can get rid of this all together. We don't necessarily need to dump this data we just want to see this being queried. So if I give this refresh let's head over to the queries section here and you can see we get a
02:31
select everything from the users table. When I give that a refresh this is now serving the cache data and then after about 10 seconds we get the new query running in. So once we've understood this if you do already know what caches are this will be pretty obvious to you. Let's go ahead and talk about cache flexible. So what this will allow us to do is now use a flexible method and we can still
02:54
pass in a key but we now have an array where we have a first value again in seconds where we consider this data to be fresh. So we can pretty much just replace out the 10 second value that we had before. What we then do is we have a secondary value which is when we consider this stale. So this is the fresh consideration this is the completely stale consideration. Now in between
03:19
this is where we can still serve from the cache but in the background it will revalidate between these two values. That means that if only five seconds have passed the user will be served the cache data absolutely. Between this the user will still be served the cache data which might be considered slightly stale at this point but in between this in the background this will defer
03:46
the revalidation to a background process. If you've already looked at the defer function that's new in Laravel you'll know how this works. After 20 seconds this will then go ahead and revalidate on the user's request. So you might be thinking well what's the point of this? Well the point is that you have a period in between here that the user still has a very fast request but in the
04:10
background it gets revalidated. That means that doing this and slightly tweaking these values depending on what you're building doing some experimenting will result in faster load times if you're using cache data because in between these periods the users will still have a really snappy experience they'll just get very slightly stale data but it will still be revalidated just
04:33
in the background. So you can play around with these values but let's just test how this works. So we need to be really careful with this and there are other ways to do this but I'm just going to refresh the page and just check when these queries come in. So we're going to go over to the app I'm going to give this a refresh just a couple of times just to get the query gone we
04:50
are now serving from the cache. Now what I'm going to do is I'm going to wait for about 10 seconds and then I'm going to go ahead and refresh the page. Okay so after this period I've just refreshed this will now be deferred to a background process to revalidate the cache and when we refresh now you can see that even after that period it still isn't performing that query to pull that data back
05:14
the data will still be fresh because the next time we run this we will get the revalidated data it's just happening in the background. Now I'm just going to keep talking for a few minutes and then after about 20 seconds we are going to go ahead and refresh the page and that will then be the completely stale data which is then going to be revalidated but as the user hits the page
05:35
so the user will then get a slightly slower request. Remember the entire point of this cache flexible method is that you can still serve slightly stale data while revalidating in the background to speed up your application. Okay so we should be good now let's give that a refresh and there we go I've been talking away we now get that query when I refresh of course we still come back
05:58
so you can go ahead and tweak and experiment with these values just remember that the period in between here is when we consider something stale but still want to display it to the user while revalidating in the background so go ahead and play around with these numbers depending on what you're building.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!