Cache Eloquent Query Results to Load Pages Instantly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today we will talk about query caching in eloquent so if you have a heavy eloquent query like this one that takes a lot of time to run how to start in the cache for some time so for that some time it will take the results from the cache instead of hitting the database and the page would load much faster and as an example I took an example from one of my previous videos about best-selling books I will link it in the description of this video so on the home page you have four top-selling books for the last 30 days and the problem is that the database is really big so 50,000 books hundreds of thousands of orders something like that and you need to take four that were sold for the last 30 days so the query runs for about a second i refresh the page now I have laravel debug bar on the bottom and this is the actual query which runs for a second let's reload again let's take a look one second something like that one second is pretty big in these days modern web development so let's cache that query for a day because if you think about it last 30 days have already passed and that result wouldn't change for a day until it's 30 days now so one running that query all you need to do is put it in the cache by doing cache remember you need to come up with some kind of string for example home page books then for how many seconds you want to store that in our case seconds will be 60 seconds times 60 minutes times 24 hours so for a day and then that query should be inside of callback function which just returns that query so you need to cut that and paste into here and that's it nothing more to change is just putting the same result into the cache and let's try that refresh nothing changed one second but now refresh again see no queries at all so that query comes from the cache and the same result is being shown but query doesn't actually happen now what if you do want to clear that cache and refresh the data there are a few ways in the code you can run cache forget and key should be the same keys or homepage books so if you're refresh now the query should run again because it forgot the cache and then it's running again or if for some reason you want to clear the cache for all the project you can just run PHP artisan cache clear command it will clear all the cache for all the application and now if we delete that line and just store that the query is running again because the cache is not present now let's clear the cache again and I will show you how it works with a smaller amount of time so let's experiment with three seconds so cache for three seconds we will wait for three seconds and I will prove that the query is still running after three seconds so if we run it immediately no query right but let's wait for three seconds we already have refresh and the query is happening again so this is the proof that that cache stays for as many seconds as you specify now how does that cache work and where is it stored so there is a file config cache by default from laravel and there is a cache driver and there are quite a few of cache drivers database file writers have stuff like that and by default it is file which means that the actual cache data is stored somewhere in the files and exactly it is in storage framework cache and there is some kind of magic how it's stored so some folders subfolders some file and somewhere inside we have that collection but you don't need to know any of that actually you just need to know what driver are you using if it's file it should be on the same server in the same file system but if you do want to store cash somewhere else in Redis or in database you need to read about that driver an official laravel cache documentation which is this page so just laravel cache and it's pretty big Paige because it talks about a lot of cash usage with different drivers so how to get values from the cash how to store them how to increment increment well a lot of stuff so you can read that I will also link that in the description but final thing I'm not sure I communicated it well enough that cash remember can store anything so in our case it's eloquent collection but with that key you can store whatever returned some text so it's not eloquent function it's an Arabic cash function and laravel collection or elephant result is just one way to use that with cash but in reality you can store anything there I hope it was helpful and be careful with cash of course in the past actually in my experience I've run into a few issues where the data was running from the cache on live server and instead of actually hitting the database and the database have been changed since then soare so be careful and double check clearing the cache from time to time to get the new data for your users if you want to know more about how to optimize your homepage for speed I mentioned the video in the beginning I've tried to optimize a shop homepage from two seconds to 0.4 of a second and the link is in the corner check it out and see you guys in other videos
Info
Channel: Laravel Daily
Views: 33,962
Rating: undefined out of 5
Keywords:
Id: JhKngeE0XJA
Channel Id: undefined
Length: 5min 43sec (343 seconds)
Published: Mon Apr 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.