Introducing Query Cache0:01
This next package is probably the most easy one to install, and it's called the Eloquent Query Cache. Who of you is using Laravel that long that you remember that you could do at some point, do something like this? You could say, give me all the users, remember them for, I think it was five minutes, or seconds, probably minutes, and then you could say this here. This would cache the result of this query, and the next time, for the next five minutes, you would get the result back from the cache.
and the next time, for the next five minutes, you would get the result back from the cache. This was built into Laravel a pretty long time ago. As you can see, it's not working anymore, but there is something pretty similar we can do through a package. Here is the documentation of Eloquent Query Cache. As you can see, Eloquent Query Cache brings back the member functionality that has been removed from Laravel a long time ago. It adds caching functionalities directly on the Eloquent level. All right, so let's take a look how this is being done.
Installing the Package0:58
It adds caching functionalities directly on the Eloquent level. All right, so let's take a look how this is being done. As I mentioned, this is a pretty small package which can be installed very easily. We just need to provide this straight, but first, of course, let's install it. Here we go, package is being installed. Perfect. Now the next thing that we need to do is provide this trait to one of our models.
Adding Model Trait1:26
Now the next thing that we need to do is provide this trait to one of our models. Again, this is working on the model level. Let's say we go here to our User, and here we're going to add one more trait, and this is called QueryCacheable. All right, so here's how we're going to test this. I'm using Ray, the debugging application by Sparcy, and I'm using this to show all the queries being done by the application, and then we're going to return maybe just all the users.
and I'm using this to show all the queries being done by the application, and then we're going to return maybe just all the users. All right, so now we need to get back to our application, and the endpoint was called QueryCache, I believe. All right, so on the left, you can see that we got back all of our users, and on the right, you can see here this was the query being made, select * from users, and then there is another one for the session. Okay, so it's working that we have made this query, but now if I'm going to refresh this,
Okay, so it's working that we have made this query, but now if I'm going to refresh this, let's just change this a little bit. We don't return the User, and I want Ray to stop sharing queries so that we only see the one that we're interested in. Maybe what I also like here is, what is it called? newScreen, I believe. Yes, so that we always see a new screen. All right, so let's check again.
Yes, so that we always see a new screen. All right, so let's check again. Yeah, we don't see results on the left, but we see here on the right, maybe we can also make this a little bigger now, that this is the query being executed. And if we refresh this, it will always show this query because this query was executed. So although we already added the traits to our application, still it seems nothing has changed for now. And that's for a good reason, because if we check again,
Configuring Cache Duration3:09
still it seems nothing has changed for now. And that's for a good reason, because if we check again, let's get here to showcase, and yeah, what we see here next to providing the traits, so this wasn't the whole story, we also need to provide a cache for. So specify the amount of time to cache queries. Do not specify or set it to null. So this is also important. So without this, nothing is going to happen.
So this is also important. So without this, nothing is going to happen. And this is being cached in seconds. All right, let's bring this into our application. Let's go back to our User model. Now we're also defining here cache for integer, and let's do it for 30 seconds. I think that's a good time for us to test it. And now if we go back to our test here, let's see what happens now.
And now if we go back to our test here, let's see what happens now. I'm going to refresh it the first time we see that the query was made, and now the second time we shouldn't see it. And I'm refreshing the page. Yeah, you can see no query is being executed. And let's wait a few seconds. So if we've done everything correctly after 30 seconds, we should get back another query. Come on, don't let me down.
we should get back another query. Come on, don't let me down. Where is it? And here we go. Here is our query again. And again, the next few times I refresh the page, we don't see the query. So this means we have now set the traits. Use queryCacheable. We've set how long it should be cached.
Use queryCacheable. We've set how long it should be cached. And this now also means that as long as we've set it for, all our requests for the User will not be executed for a query. We will get it back from the cache. And yeah, it's pretty easy to install this inside your model just by using the trait and the cache for how long you want it to be cached for. And from now on, for every query you're doing, this will be stored.
And from now on, for every query you're doing, this will be stored. I think this is pretty amazing how this works. And I'm using this trait in two applications that I'm currently working on. And I find it very useful, especially if there are many queries being made and you don't want to define for every one of those queries that you want to cache them manually, you can do this with this trait.
Using Advanced Cache Options5:17
that you want to cache them manually, you can do this with this trait. But of course, you have way more options to use this. If we go back to the documentation, and let's go to query caching, there are some more options that we can use. And then what we also can do is don't cache. So let's maybe try this as well in our example. So first, we're going to have to query again. All right, so now at this point, we don't see it.
So first, we're going to have to query again. All right, so now at this point, we don't see it. Now let's use don'tCache, get. And I have to be quick enough. We should immediately see it that we now see another query here, which was executed. All right, so this is working. And you can also define, especially for some calls again, cacheFor, and define it yourself. You can define a prefix as well.
cache for, and define it yourself. You can define a prefix as well. So many things that you already know from caching you can do here. But yeah, I think the most interesting aspect of this is that I can use it on the model direct itself, and all queries are being cached for a specific time, of course. Let's break down what we just learned. With Eloquent Query Cache, you can easily set up caching for your Eloquent models. And the most easiest thing about this package
you can easily set up caching for your Eloquent models. And the most easiest thing about this package is that it out-of-the-box works for all queries, and you don't have to define this for every call that you make. Thank you, Alex Reynaki.
