YouTubevideo· May 21, 2024· 9:59· 18K views on YouTube

Laravel and Redis: The Absolute Basics

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

24 moments
  1. redus it's a pretty hot topic right now and Licensing and branding aside it's pretty useful but how do you use redus within laravel are there multiple ways to use it let's take a look and then we'll do some code to see exactly how to use redus with laravel now we're not going to be diving into the specifics of how to set up redus on your local machine to be used with Lille but we are going to be diving into how do you use reddis how do you

  2. write the code to use reddis as a key value store and as a caching database so I would advise looking at the larel documentation the reddis page for that for specifics on how to set up reddis on your local machine as well as the reddis documentation now one thing to note is you can use multiple databases within Lille so even if you're using reddis outside of caching for something like a key value store which we're going to take a look at the specifics on how to do that in a little bit you can also use

  3. multiple reddis connections within a single Lille application so this is useful if you have one connection that is going to be used for caching and another connection that is going to be used for a key value quick database while redus is largely used as a cache or session driver you might have a

  4. specific use case in mind for an inmemory database like redus where you can use the examples that we're going to be going over for that part particular use case additionally larvo allows you to set up reddits as his Q driver as well for this demo application we're going to be taking a look at what a live application dashboard might look like to a bunch of different active users across your social media platform so we want to

  5. show active stats in real time or as close to real time as possible now this polling that is happening right here is just using live wire pull option so that you wire cool in PLL directive and then that's just to signify or to kind of show the amount of traffic that a certain website could have now every

  6. time that this is updating we don't want to have to make a database call ideally we'd love to pull from the cache first and then maybe just update the cache every 60 seconds so what does this look like in the code and then how can we change this to use redus to cach these options so we have a Livewire volt component and this with function up here is basically just saying anytime the component reloads we're going to return this information in this case specific database count queries for user for

  7. active users and then for discussions now we're just using this wire pull directive to signify and kind of show if a bunch of different users were accessing this page all at once how would that affect our queries well they're going to be very expensive cuz not only do we have to to serve up that request to the user but now we also have to make that expensive database query to just get the count well this cash is going to be the

  8. best way to do that within reddish so let's take a look at how we can Implement that okay now grabbing the user count adding it to the cach and then using that anytime we load this Live Wire component is going to be fairly simple we can just say we want this user count uh to be part of the cach and we're just going to say remember and we're going to say um we'll just call this uh user count now we can

  9. set this to 60 seconds and then the return function is going to be what this should pull from Once those 60 seconds have expired in this case we're just going to return the user count now that's what we're using down here so now we can just pass along the user count

  10. variable it's as simple as that now we have this item in the cache and it's going to be updated every 60 seconds I'm going to zoom in real quick so you can see this but what you're seeing here is the cache item that we created laravel database user count larel is doing this naming behind the scenes we just specified that user- count variable and that TTL there at the end is how long

  11. until this has expired I'm going to run PHP Artisan new user user which just says heyy give me a new user now the cash hasn't exactly expired yet so we're not going to see that new user reflected for another 60

  12. seconds one minute later and there you can see that cash was deleted and we pulled the new number in now this is where reddis caches can be incredibly valuable you might not need need the upto-date information every single time a user requests that information from the page but now you have an easy way of

  13. saying when has that cash expired and when do I need to pull additional information now what if we want to use reddis for something outside of cache or Q's or our session driver which Lille provides for us out of the box we just have to set it up in our environment variables for example session driver this would be reddis we could have the reddis client um set here and then we

  14. would just have um any kind of cach store would be redus instead of database and Q connection reddis but what if we want to do something as simple as grab a specific variable from the database and display it somewhere or set a specific variable from the database now this is a key value database which means it needs to be unique keys but if it's something that we're easily just setting and grabbing this is how you would do that in Lille let's open up our set reddis Lille Live Wire component and we just

  15. have a uh stored value now this is where we can get a specific variable from our reddest store we want to get Fu well Fu doesn't exist because if I was to delete this stored value variable we have nothing so how can we set something well let's go ahead and bring that back we're just going to set this to initial string and maybe we want just set up a um input

  16. here where we're going to say actually let's go ahead and set it up down here where we want an input with that's wire modeled to the stored value we'll just go ahead and kind of divvy out some

  17. classes for that okay testing one to now we don't have a button or anything to kind of set this variable so we're going to go ahead and input that in here we're just going to say um first off we want to close that off now we want a button that just says um add stored

  18. value perfect we add some classes to make this look a little bit prettier okay not the prettiest but if we want to um then call this we can say wire click

  19. um add stored value and why don't we create that function here so public function add stored value and we can set the Reddit variable Fu to the stored value that we're creating here so if I was to type we go ahead and refresh this just in case if I was to type um a new one

  20. store great we have this being stored in reddis you can see here this actually setting as a new database um where we have data LEL database Fu a new one which is a string and there's no expiration date for this particular stored value if I was to change this

  21. then we have that same value being retrieved refresh and we don't have the variable anymore let's see what happened okay Ah that's why because we're storing we're setting the stored value on the width with is which is ready get but what we actually want is this stored value see what that looks like this stored

  22. value there we go perfect so now we're storing the value with this wire modeled \[Applause\] input and we're pulling that value from the reddis getter as well again this is a key value pair so that value needs to

  23. be unique in your reddis database Lille gives you some Nifty ways to connect to reddis whether that's cache session drivers or a q driver but Additionally you can use Red's get and set values within your lville Live Wire components or just lville itself to be able to get and set key value Pairs and use them within your application so now you have a fast way of storing these settings in

  24. memory and a particular database like redus \[Music\]

just you here