تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Create Laravel App0:00

So you've learned a bit about socket.io, but now let's switch over to Redis. This is how we're going to handle the PubSub aspect when it comes to broadcasting something from the Laravel framework and being able to pick up on it from our Node application. So let's create a new Laravel application, and we're also going to do this through Homestead, and that way we get the Redis CLI straight out of the box, and we don't have to install anything. laravel new Redis lesson. And next, let's set up a host here. So I'm going to go to my /etc/hosts file, and right down here at the bottom, let's create a new one called Redis.lesson.

So I'm going to go to my etc/hosts file, and right down here at the bottom, let's create a new one called redis.lesson. So let's go into my VM using vagrant ssh, or I just have a little shortcut here called VM. Go into our code, redis lesson directory. And next, let's set this up. So using Homestead, I can say serve, and if we just run that as it is, you'll see that we specify the domain name, which is redis.lesson, as well as the path to where the project root should be. So in this case, the project root should be this full path and also the public directory. Okay, serve redis.lesson, and add that path.

So in this case, the project root should be this full path and also the public directory. Okay, serve redis.lesson, and add that path. We will restart Nginx. So we should be all set to go. Let's go ahead and take that example .env file and just make it our official one. Finally, let's do the typical php artisan key:generate. And yeah, let's go ahead and try this out in the browser. If I visit redis.lesson, sure enough, we see the welcome page. Great. So I'm going to visit our project in Sublime.

Redis and Caching Overview1:31

Great. So I'm going to visit our project in Sublime. And what's cool is Redis support comes straight out of the box. In fact, you have a Redis facade available if you didn't know about that. And further, you have a Redis driver when it comes to caching. Let me show you. If we go to config/cache, you'll see that obviously out of the box, the default driver is just files. However, you can swap this out for memcached or Redis if you prefer. That means if you were to say cache put $who = bar,

However, you can swap this out for memcached or Redis if you prefer. That means if you were to say cache put $who = 'bar', behind the scenes, that's actually going to use Redis to store that. And remember, Redis is just a nice in-memory key value store that has persistence built in, which is why so many people like it. It's pretty useful. So before we try this out, let me show you something. Let's go to our routes.php file and just try to use the Redis facade. Let's pull in Redis. So we will import that and then do something as simple as this.

Use Redis Facade2:24

Let's pull in Redis. So we will import that and then do something as simple as this. Redis::set. And now the way it works is Laravel just sort of defers to the core Redis API. So whatever static method you reference here will just be passed on to Redis. So if I were to say set a key called name and make it equal to my name, and then just for example if we returned Redis::get('name'), it should say Jeffrey once we have all of the dependencies installed. Let's try it. If I go to Chrome and we run this,

Install Predis Dependency2:54

Let's try it. If I go to Chrome and we run this, you'll notice we get class predis or predis/client is not found. And that's because if you want to use Redis in a Laravel project, it has a dependency that is not included out of the box. If we visit the documentation for Redis and if we scroll down, you'll see that we need to pull in this dependency through Composer. So let's do that now. composer require. And there we go.

composer require. And there we go. So let's go back to Chrome, give this a refresh, and we should see my name. And we are successfully using Redis. And in fact, we can verify this. If we go into our VM, because we are using Homestead, Redis is automatically installed for us, which means we can play with Redis right now. So for example, if I want to see any keys that I personally have, we've only set one name and we see it here. So if I get name, it says Jeffrey.

Explore Redis CLI Commands3:42

we've only set one name and we see it here. So if I get name, it says Jeffrey. If I set name equal to John, and if we get name, now it's John. Or maybe we want to set on a hash. So for example, HSET. And oh yeah, on that note, unfortunately, if you're not familiar with Redis, I feel like the API is actually pretty confusing because everything is abbreviated. So in this case, HSET, while we're saying we want to set on a hash. But yeah, you'll see a lot of things like this, where in my mind, it's just not the most readable API ever.

But yeah, you'll see a lot of things like this, where in my mind, it's just not the most readable API ever. But anyways, maybe we have some hash called preferences, and we want to set a length preference to 30. Okay, now we have that. So once again, HgetAll, get all items in this hash. And if we run it against preferences, we have two. If we want to get a specific one, I could say preferences and what is my length. And there I get 30. Or another example might be, let's say you want to increment the length.

And there I get 30. Or another example might be, let's say you want to increment the length. Well, you could say H increment by. So you see what I mean where everything's abbreviated, and it just doesn't make for very readable code in my mind. Hash, I want to increment by. So we reference our hash, preferences. Then the field we want to increment. And then finally, the by, how much do we want to increment this by? One is fine.

And then finally, the by, how much do we want to increment this by? One is fine. So now that's set to 31. And if we fetch it, you see it here. So now using that Redis facade, imagine we want to fetch this exact value. Okay, well, remember how I said this just delegates dynamically to any commands on the Redis API. So I could say return Redis::h.get(preferences). And then, yeah, we just delegate to all of the options we have here. So in our case, we want the length.

And then, yeah, we just delegate to all of the options we have here. So in our case, we want the length. So we would say preferences.length. And that should return 31. Refresh, and there you go. Now further, like I was saying earlier, if we visit our config/cache page, we have set our default cache driver to be Redis. But now remember, in this case, this is actually referring to the default. So don't forget in your .env file, we're actually overriding this. So that's why it's good for this just to be a fallback.

So don't forget in your .env file, we're actually overriding this. So that's why it's good for this just to be a fallback. And then your specific request will be in your .env file, which will be Redis. Okay, so now in our routes file, I could say cache put foo bar, and that will live for 10 minutes. A key called foo, a value of bar, and you can delete this after 10 minutes. So if I now return cache get foo, we should, of course, see bar on the page. And we do. And further, we can inspect this like we've already reviewed. Redis CLI, let's review all keys.

And further, we can inspect this like we've already reviewed. redis-cli, let's review all keys. And now you can see anything we use with Laravel will be prefixed with Laravel colon. So if I were to say get Laravel foo, and yeah, there we go, bar. And in this case, Laravel is just serializing it. And when you do cache get, it un-serializes it. So now one final thing I want to show you is, because we have that unified API for caching, if we were to check out, for example, a Redis store, this would be the Redis implementation, and something as simple as get, well, you can see we get our Redis connection, and we call that get method.

this would be the Redis implementation, and something as simple as get, well, you can see we get our Redis connection, and we call that get method. That is the same as you would reference within Redis. Or what about something with incrementBy? Yeah, if we were to say cache increment, a key by one, well, behind the scenes, it's going to, once again, reference the Redis API, which is incrby, increment by. But yeah, mostly you don't need to worry about any of this stuff. It's good to know you can set the Redis driver. You can rest assured that behind the scenes, you're now using Redis for that. Or of course, if you do need to reference Redis directly, you can do it.

Plan Node Redis Client7:26

You can rest assured that behind the scenes, you're now using Redis for that. Or of course, if you do need to reference Redis directly, you can do it. And of course, that's great for things like scoring and tons of cool stuff. Okay, so now that we have Redis set up on our Laravel end, well, on our Node side, we also need a Redis client. And that way, from the back end, we can publish something using Redis. On our Node end, we can listen using a Redis client. And then when we pick up any kind of message that was sent, for example, a user signed up event, we can catch that on our Node end and then use socket.io to emit that to the client side.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟