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

Using incrementBy0:00

So, in the last episode, I demonstrated the most basic possible example, where you increment a number. So, we could say, increment visits by 1. But then I also showed you the Redis commands list, so if we look for increment, there's also an incrementBy. So this means, if you don't want to increment by 1, but maybe you want an increment of 5, well we can use that method. And we also learned that with Laravel, the Redis facade, any static method is just going to defer to that API. So I could say, increment visits by 5, and now if we were to hit this in the browser,

to defer to that API. So I could say, increment visits by 5, and now if we were to hit this in the browser, and then switch to the terminal, open up redis-cli, and then we get visits, now it's set to 6. And I assume that's because I hit it one other time. So let's delete it, run it again, and now we should get 5, which is what we would expect. But for every visit, it's now going to go up by 5, rather than 1. So that's really good to know. You can review that commands list, and then instantly use them like so. But yeah, a site visitor counter, I mean, who's doing that these days?

Real-World Counter Examples0:55

You can review that commands list, and then instantly use them like so. But yeah, a site visitor counter, I mean, who's doing that these days? Not overly applicable to most sites. But the concept of a counter is still completely applicable. Think about it. The number of Twitter followers, well that's a counter, right? How about, at Laracasts, the number of video downloads? So if you go to some lesson, well maybe there could be a count that says, this video has been downloaded 5 million times, in my dreams. Yeah, I mean, I could store that within MySQL, like with the lessons table.

Namespacing Redis Keys1:54

There's lots of examples of this. But we do have one problem. Earlier, we were just using a key of visits, and that's fine for site-wide. But if we want to do, for example, downloads per video, like at Laracast, well, downloads is too generic, right? We need to namespace it in some way. But the problem is, yeah, Redis is just a NoSQL database, just simple key values. So what we have to do is sort of like the way we fire events in Laravel. We're going to fake a namespace, either by using colons or dots. I generally use dots with most of my Laravel stuff, so I would do that with Redis.

We're going to fake a namespace, either by using : or .. I generally use . with most of my Laravel stuff, so I would do that with Redis. But I think it's probably more common, actually, to use a : as your namespace separator. So for example, if you had users with the ID of 1, and we want to store their follower count, we could do that here. Or if we have videos that has an ID of 2, and we want to store its downloads, yeah, notice how we're namespacing that. That way, this one can be unique to that one. Let's see that in action. We're going to go to videos/ and then just some kind of ID, where we grab that.

Displaying Video Downloads2:57

Let's see that in action. We're going to go to videos/ and then just some kind of id, where we grab that. And we'll say $downloads will be Redis::get('videos.' . $id . '.downloads'). Now on the condition that this does not exist with Redis, null is going to be returned. So keep that in mind. Then we would load a view. I'm just going to use the welcome view that is included with Laravel. And if we come down here, we could say some video, and then say this video has been downloaded downloads times. And let's pass that through with $downloads.

downloads times. And let's pass that through with downloads. Okay, so we hit this page. We search Redis for a key of this name, and then we pass that variable, whether it's an integer or null, to our view, and then we echo that out. So on the condition that this is set to null, well, maybe we could just do something like this. This video has been downloaded no times or five times. Okay, so let's try it out. So back to Chrome, videos/1, and it has not been downloaded.

Incrementing on Download Route4:03

Okay, so let's try it out. So back to Chrome, videos/1, and it has not been downloaded. Okay, well, let's simulate a download then. If we were to say Route::get('video/{id}/download'), we're just going to stub this out really. We'll accept the $id, and we would do something to present the user with a download link. Maybe we redirect them somewhere. Maybe we zip it up, whatever. So prepare the download. But as part of that, we're also going to increment the number of downloads for this video.

So prepare the download. But as part of that, we're also going to increment the number of downloads for this video. So we could say Redis increment, and then once again, videos id downloads by one. And that's it. We just increment that value, and we can redirect or return. Let's just redirect back. That's fine for the example. All right, so now if we hit this URI, it will simulate some kind of download. But as part of that, we will increment this key, and then we go back to the previous page. So let's try it.

Inspecting Keys in Redis5:21

And now we have one. And let's do one more. Video with an ID of 6, of course, has none. But if we download it a couple times, now we have two. Now, of course, we can verify this using the redis-cli. And I'll show you a new command you can use. It's called keys. I'll mostly just use this for inspection. Try not to use it for production. But the way it works is you can provide a regular expression, and it will return all

Try not to use it for production. But the way it works is you can provide a regular expression, and it will return all keys that match that. So in this case, yeah, video.star, well, that's going to return three in this case. Now if we were to go to video 10 and download it, that will create a new key, right? So if I run it again, now we can see it right there. Okay, so we're definitely taking baby steps here. But think about what you've learned. We now know, yes, we can use increment, that command. But we can also use an incrementBy command.

We now know, yes, we can use increment, that command. But we can also use an increment by command. And further, while there is no concept of namespacing here, of course, we can simulate it by using a period as the namespace separator, or colon, or anything you want. But those two are the most common. And then finally, while we learned that a site visitor counter is kind of lame, in real life for most projects, you are counting countless things. Once again, like followers, or downloads, or visits, or blog views.

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