در حال بارگذاری ...

Installing Redis Locally0:26

However, maybe you're using Laravel Valet on the Mac, which I would highly recommend. It's great. In that case, you would want to install it through Homebrew, which will be available to you on the Mac. So in that case, you would say brew install redis. So yeah, once again, on Homestead, it's available out of the box. For Mac, use Homebrew. And for any other situation, you can just install it through the site. It's not too difficult. You can figure this out. And if you need help, just leave a comment below.

Adding Predis via Composer0:50

It's not too difficult. You can figure this out. And if you need help, just leave a comment below. Anyways, like I said, I will be using Homebrew. And I do want this to be up and running every time I restart my machine. So I will paste that in. Okay. Next, if we switch to the Redis documentation for Laravel, we do need to pull in the Redis library. This is kind of the de facto Redis library for php. So let's pull that in through Composer.

Setting Up Redis Route1:10

This is kind of the de facto Redis library for php. So let's pull that in through Composer. And mostly, we'll be all set to go. Okay, so if we switch over to Chrome, let's close these out. And you can see that I've installed a fresh copy of Laravel 5.3, but haven't done a thing. So we're going to go to Sublime. We're going to go to our routes file. And right here, let's leverage the Redis facade. And I will import this at the top.

Incrementing a Redis Key1:31

And right here, let's leverage the Redis facade. And I will import this at the top. Eliminate support\Facades\Redis. And we're going to defer to a method on the API called increment. And that stands for, of course, increment. And we'll give it a key here called visits. So yeah, Redis is a simple key value database. And you're going to find in certain scenarios, it's a much faster and a much maybe more of an appropriate solution over using MySQL.

it's a much faster and a much maybe more of an appropriate solution over using MySQL. And I'm going to show you a lot of examples of that. So now, naturally, this visits key doesn't exist yet with Redis. It's the very first time. So in this case, we call increment on visits. Well, it'll create that and default to 1. And what's nice is Redis is persistent. So on the next page load, it'll default to 2 and then 3. So let's run that.

So it gets the value. It was currently 1. And then it incremented that. So it became 2 and then 3. So you can see with literally one line of code, you can have one of those old school visitor accounts for your website. But actually, beyond this dummy example, there's a number of use cases for something like this. But, yeah, to finish this up and to put a bow on it, we will load the welcome view.

Displaying Visitor Count2:49

But, yeah, to finish this up and to put a bow on it, we will load the welcome view. We'll send through the visits. Or you can use this dynamically like this. And let's go to the welcome page, clear all of this out. This is all the intro page that you can delete. Okay. We will say, hello, you are visitor number or number. And then we will echo out the visits. So come back to Chrome.

And then we will echo out the visits. So come back to Chrome. Give it a refresh. Visitor number 5, 6. But if we load this in Firefox, there we go. This person's visitor number 7, visitor number 8. Okay. So in this lesson, you installed Redis. You pulled in the Predis library through Composer. And you referenced your first method.

Exploring Redis Commands and CLI3:32

You pulled in the Predis library through Composer. And you referenced your first method. Now, if you want to take a break and get a head start before the next episode, have a look at redis.io slash commands. And basically the way it works is with that Redis facade we've been using, any of the methods we call statically, they're just going to defer to the API you see here. So if we look for INCR, let's take a look. There it is. This is the method that we called.

There it is. This is the method that we called. Now, if you come down here and you see things like this, so set $myKey, well, that's not quite the way we worked. You can try this stuff out on your own by using the Redis CLI that will be available to you. So in this case, if we want to set a key, increment it, and then fetch it, well, it looks like we can use get and then the name of the key. So get visits, and that will be 8. If we increment visits, now it's 9, 10. Get the visits or delete the visits to start from scratch.

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