Laravel - Caching Made Easy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone and welcome back to lair Academy it's make again and in this lesson I wanted to cover caching within larval I know that this is something that I've done previously however in the latest version or a couple of versions before the 5.7 level has made some great strides and caching and made it super simple and easy to do there's no reason why us developer shouldn't understand caching or how to do it within your a level app now you might be wondering why you would cache a query or cache something well for instance right here on this page I'm showing approximately 15,000 users this is something that we would probably cache as long as these these users or this information isn't going to be updated if the user is going to update their information then I'll have to clear that cache and then re cash at the values it's not really that big a deal with the way that layer of law works you might also be caching a report that runs pretty often and the parameters don't change or you might catch something like an image that you're grabbing from a different server there's a whole bunch of reasons why you may catch something and this is just the start of it now if we take a look at the example project that I have in front of me I have some random users and if I hit refresh you can see it goes off to the distance and then basically once it's done it will be displayed the 15,000 users now that didn't really take that long because I'm not joining on any other tables if I had another table to join to and grab that information well then that would start taking quite a long time but like I said in this video what I want to do is I want to take a look at caching so let's load up our IDE and see what's actually happening right now inside app HTTP and the controllers I have a user controller and right here I'm describing all of the users and I'm ordering by the name and then I'm just making sure I get those records then I'm just passing those users into my blade file itself so how would we cache these users here well I think I've covered this one before but what we should be using is called a repository we can actually just make a new folder underneath and we will just call this repository if I could spelled that right then within this folder we're gonna make a new file and in here I'll just call it user let's call it users about PHP now we're gonna have to make sure that we namespace it so I am within the app slash repositories and then the next thing we need to do is start off with our class and we'll call this users and that's just because of our file name here now what do I want my users to do normally I would have something like a method called all and I could pass in and order by and then I'm later down the road maybe I'll have a method called yet I can pass in the ID but right now let's actually just focus on the all method now because of the way that caching works we need some unique identifier the look of the cache itself so in situations like this I may create a constant variable I'll just call this yeah let's call this cache key and let's set it to users and that will be it now I may have another function somewhere in here and let's make another one called let's say get cache key which didn't I pass in will pass in a key itself and all this is going to do is return the constant of the cache key and then I'm gonna add on to it a period followed by the key itself actually what I'll do is I'll take that key and I'll run STR to upper just so we can kind of standardize and have everything uppercase great so even though we have this here this is just going to be our gateway into the cache itself so we can kind of ignore this a but it'll be very useful going forward now if we wanted to display all the users well let's actually take what we have in our user controller which is this guy here and let's first initially put it into our repository so we can clean this up by saying we're just going to return all the users ordered by the variable that we pass in great now let's make sure we import this so we'll say use app slash users or sorry user and now in the user controller we can take out these sorry this issue statement we could use app slash repository slash users now when we grab this information all we have to say is I want to access the users repository and I want to call the all function and now I want to pass them the name itself now we made a repository called users but I'm using it with a static function fell here how does this work well right now it actually won't if we try and run this we're gonna get an error says if we cannot call a non static method even though we're trying to write here to fix this we can add the word façades in front of it and now laravel will automatically new up this repository class whenever we call it and then that will allow us to run the all function here let's check it out if I hit refresh you can see that now all the users are coming back just like the word before okay so this is great we're back to where we were before we made the repository now how do we start caching this information well if we switch back in sublime text and go to our repository we know that we now have access to a cache key so okay let's make the key equal to this get cache key and the cache key that I'm gonna be using is actually going to be - so I'm gonna say cache key is equal to the word all followed by the variable order by now I put this in curly braces just to help PHP figure that out so if we were to actually die and dump this cache key let's see what we get here now remember that we are ordering it by well we have expected returns that's mainly missing a semicolon which is right here we'll comment that out for now let's switch back and do a refresh so we can have all done name and that is because we're passing a name as the order by if I come back here and the user controller and I ordered by email now the key is going to be all dot email so the reason why I'm doing this is because maybe a different section or we allow the users to sort depending on what table fields they click and what if we're not using javascript we'll have to have PHP do it but this will make it so that we don't continuously clear the cache but specifically for the sorting order that cache may or may not already exist so I'm gonna switch it back to name and now that I have my cache key itself I need to actually get the final cache key so if we come back and actually let's kind of just mmm switch these variables so the key is gonna be all dot whatever we pass in and the cache key is going to use our function and then pass in the key itself so once again let's actually take a look at that a variable but save everything and switch back to Chrome and refresh and you can see that now we have users all dot names so this is going to be our access into the cache itself so they use this well like I said level has progressed and caching a tremendous amount and it makes it super easy for us to do so instead of saying return the user all we have to do is say return and I'm going to access the helper function called cache and then call the function remember so remember takes three parameters the first parameter is going to be our key into the cache which is gonna be our cache key and the next parameter is going to be how many minutes or how long or when do we want this cache to expire I'm gonna go to the top and I'm gonna pull in carbon I'm gonna say use carbon carbon that allows me to say for the second parameter I could say carbon now then add minutes let's say I cash this for five minutes so we can even have some like add days add hours etc such a carbon just makes it super easy we don't have to do any math to figure out when it's going to fire another third parameter this is going to be called it's a closure and it's going to be called when the remember function cannot find the key that we're looking for or the cache has expired so in here we'll write a new function and what we want to do is we want to pass in the when you pull this over we're gonna have to pass in this order by a variable just so we can use it because of scope now all we have to do is say return what we wanna return when we return the user order by and then well use our variable order by and we'll say get now automatically what's going to happen the first time we run this it is not gonna find a cache key and it's going to run this closure here and it's gonna return all of the users ordered by whatever we passed in and then when we hit refresh again because hopefully it will be within five minutes it's gonna find that cache key and it's just gonna return the cache itself so here let's try this if we come back and we refresh we know this is going to go toward database so this is gonna take some time to finish a lot of this is rendering to the page but once that's done if we hit refresh again hopefully you'll see that it's a lot faster and it's a little hard to tell here but we're not accessing the database or anything like that for example let's actually choose one of these emails and make a change if I load up tinker with PHP Arsene tinker I will grab the first user app whoops user where the email is equal to that email there and I believe it's email address so I'm right and what we want to do is we want to change the name to equal Mickey and then we will save that information now remember if we do a search we're looking for this email here if I hit refresh this email is I'm sorry this name is gonna stay the same even though it's associated with the email address at the top now we know that that didn't go to the database because if I come back and I run PHP artisan cache clear that's gonna clear all of our cache and if I hit refresh the first email here that comes back I'm sorry when we look for this email you can see then it's gonna be down here and this is definitely the email that we have been fooling around with so hopefully that gives you a little bit of an example of caching and just you can see how easy it is and how simple it is to do now again if we wanted to do something for the get well all we could do is make sure that we have our keys in here so we could say get and then we might as well say the ID and then all we have to do is we can copy this return paste in here and say okay remember the cache key which will be users get based on the ID will cache it for five minutes and if it can't find it then we want to run this anonymous or sorry disclosure here using the ID and we get to say fine ID and let's say we had a whole bunch of relationships well in here we could also just say we could load them up we could say with and we could say let's say we have the emails in a different table and then we could say where the ID equals ID and first and then what this would do is that would load up the relationship automatically and then it would cache it so if we have a whole bunch of different relationships we can load them all once and then once the cache has kicked in it automatically goes through and does everything for us anyway this was supposed to be a short video but looking at the timer it seems a bit long I just want to show you how quick and easy and really amazing it is cash within laravel now I would like to thank you for watching and hopefully you learned a few things please like comment and subscribe on YouTube it would really help me own thanks again for watching
Info
Channel: Laracademy
Views: 35,563
Rating: undefined out of 5
Keywords: laravel, caching, laravel 5.5, laravel 5.6, laravel 5.7, php, tutorial
Id: 34zQR21MIoU
Channel Id: undefined
Length: 13min 19sec (799 seconds)
Published: Mon Sep 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.