Introducing Redis Hashes0:00
Now, at this point, we've learned about basic set and get values, working with scalars. You've learned about lists and sets and sorted sets, at least slightly. But we haven't talked about hashes yet. So you can think of a hash as an associative array. That's at least how I think about it. So for example, if we wanted to get the User with an ID of 1 stats, just using a php pseudocode. Okay, well, maybe their favoritesCount would be 50. Their watchLatersCount would be 90. And then maybe completions, using like a Laracast example here. Yeah, this is the sort of stuff that you, if you're not careful, you end up calculating.
it. But yeah, then what about when there's another little stat count you want to grab, and then another one, and then another one. If you're not careful, you end up adding more and more columns to your table, and it gets kind of messy. So instead, if we used Redis, it's a lot faster, it's a good choice for things like this. So if this is an associative array, what would be the equivalent in Redis? Well, let's do it in the command line and then switch over to our php. We could use either hset or hmset. So once again, it begins with h because we are working with hashes, so that is the prefix.
We could use either hset or hmset. So once again, it begins with h because we are working with hashes, so that is the prefix. So the only difference between these two is the m stands for multiple or mass. Or in other words, hset would be to set a single item within the associative array, so to speak. hmset would be if you want to set all of the items. So you'll use hmset quite a bit. Let's try it. Help, hmset, and it looks like syntax highlighting is off, but we want to give it the key. So in this case, that would be some form of this.
Setting Hash Fields1:54
Help, hmset, and it looks like syntax highlighting is off, but we want to give it the key. So in this case, that would be some form of this. Next, the field, that would be favorites, and then the value would be 50. Now we use hmset when you want to continue with multiple values. So let's do it. hmset, user with an ID of 1 and their stats. Well, specifically, we want to set the favorites count to 50. What else? The watchLaters to 90. And then finally, the completions to 25.
Retrieving Hash Values2:24
The watch laters to 90. And then finally, the completions to 25. And we get an OK. All right, let's use hget to fetch that. User.1.stats. And how many favorites do they have? 50. And how many watch laters? So notice when we use hget, we have to specify the field name. So we have to get a specific item within the associative array.
So notice when we use hget, we have to specify the field name. So we have to get a specific item within the associative array. Once again, using terminology that's a little more familiar to us. If we want to grab everything, we can use hget all. So basically, give me that associative array, and that's what we get. So that's the equivalent of saying, just give me this right here. OK, so let's switch over to our php. And once again, what is the equivalent for this? If you're having any trouble at all translating things over, remember, it's very, very simple. Each key here translates to an argument to your function call.
Using Hashes in PHP3:11
If you're having any trouble at all translating things over, remember, it's very, very simple. Each key here translates to an argument to your function call. So for example, I could say return. We import Redis. And I could say return Redis::hgetAll('user.1.stats'); And let's see what we get. Come back to Chrome, and there we go. We have our values. So that means if we want to grab the favoritesCount for this User, refresh, and there we go. And again, because we're using this form of namespacing here, user.1, well, that means
So that means if we want to grab the favorites count for this User, refresh, and there we go. And again, because we're using this form of namespacing here, user.1, well, that means later if you want to set up a user.2.stats, you can do so, and they won't interfere with one another. Let's try that. We're going to say user.2.stats, and then Redis HMSet user.2.stats. And we will pass our associative array here. Then we will return that person's stats. So put it into the database, and then fetch it, and return it to the screen. Now we can see user.2.stats. And I should probably change these to make it a little more clear.
Now we can see user.2.stats. And I should probably change these to make it a little more clear. So let's do 10, 20, 30, 30. Okay, anyways, one more time. And whoops, user.3.stats. Anyways, now we can see theirs. So maybe this sort of thing is public, and we allow folks to go to, for example, how about users/${id}/stats or their profile, and we're just going to grab that. So we'll say $id, and then we'll say, yeah, just return to me Redis::hGetAll("user.{$id}.stats"). Okay, so we could say users.1.stats, and for number three, very, very fast.
Incrementing Hash Counters4:49
So we'll say ID, and then we'll say, yeah, just return to me Redis::hGetAll(user.ID.stats). Okay, so we could say users.1.stats, and for number three, very, very fast. And we're not having to calculate this junk on the fly. But now what about when the User with an ID of one favorites a video at Laracasts or something? Well, once again, we're back to simple counters here. As part of that form submission, you would either fire an event or do it right from your controller to increment their favorites count. So that would take the shape of something like this. We're just going to use a GET request here to make it quick. When they hit some URI to favorite a video, we'll say Redis::
We're just going to use a GET request here to make it quick. When they hit some URI to favorite a video, we'll say Redis. And once again, with hashes, we have hincrby, or with sorted sets, we had zincrby. So you can see there's a lot of overlap here. Once again, user.1.stats, we are incrementing which field, the favoritesCount, and how much are we incrementing it by? Of course, one. So now we will return a redirect back to just the home page if we want. And we'll get rid of this example here. Okay, so take a look.
And we'll get rid of this example here. Okay, so take a look. We don't need any of this. What you could do, by the way, if you're trying to build up the initial database for your users, this is something that could maybe go within a artisan command. So you do the initial logic, and this would defer to something like the authenticated users favorites count. This is where you reference your actual queries. User watch laters count. And presumably all of those are like pivot tables.
User watch later's count. And presumably all of those are like pivot tables. User completions count. So you do all of these queries, you throw it into Redis, and then you only have to run that one time when you push up your project. Then beyond that, whenever you have to change the value, you would just use incrBy or decrBy. So let's grab the user.1.stats and try it out. All right, they have 50 favorite videos, but now they're going to favorite some random video. And now they have 51, and you get the basic idea.
video. And now they have 51, and you get the basic idea. What if we switch back to the User with an ID of 3? Okay, they've only favorited 10. So now when they favorite the video, once again, this would not be hardcoded. It would be something like the authenticated User’s ID. All right, you get it. So anyways, when they favorite the video, now they have 11 favorite videos. Now, that about does it for this initial review of hashes. If we switch over to the php artisan commands, there's a lot more here.
Laravel Cache with Redis7:47
of them. But I do want to touch on one thing as we finish up. You may know that with layer file, we can use the Cache facade. And you can specify in your config section for cache, which driver you want to use. And you can see, of course, that we can set it to Redis if we want. Okay, so if we were to change that, we're going to use Redis now. That means whenever we use the Cache facade. So if we were to say cache put foo bar for a period of 10 minutes. Yeah, now that's going to use Redis, and we can grab that. So come back to Chrome, and sure enough, we put it into the Redis database, and then we
Yeah, now that's going to use Redis, and we can grab that. So come back to Chrome, and sure enough, we put it into the Redis database, and then we fetched it out. But real quick, this may confuse you, because you may try to switch over to the Redis CLI and say get foo, and you get nothing. And that's because Laravel by default applies a prefix. So for example, back to Sublime, if we go back to our config/cache.php section, there should be a place for the prefix. There it is. So that means it'll, by default, apply a prefix to every value.
There it is. So that means it'll, by default, apply a prefix to every value. So we would do layer file foo, and now we grab the value. But here's the next thing. Notice that it doesn't just say bar, it is a serialized version. So this is what layer file does to keep it fairly simple. And it actually works very well for lots of use cases. And it keeps the API really basic. You'll notice with cache, you're not using sorted sets, and you're not using hashes the way Redis provides.
You'll notice with cache, you're not using sorted sets, and you're not using hashes the way Redis provides. It kind of normalizes it. So what layer file is doing, for example, if I were to say hput foo bar, behind the scenes, and I'll show you this in a minute, it's basically going to serialize bar. So that's why you can use any kind of data type you want. For example, name is LaraCass, and its age is three. OK, so this all works because behind the scenes, it's going to serialize it. And then when you do cache get foo, once again, it's just going to un-serialize that array. Kind of a nice, simple way to go about it.
And then when you do cache get foo, once again, it's just going to un-serialize that array. Kind of a nice, simple way to go about it. So that means if we were to come back and give it a refresh, we still get our array. But again, in the database, it's being serialized. And if you ever want to inspect this for yourself, just go to Laravel's Redis store. So this is what implements the default API for Laravel's cache functionality. So things like get, put, putMany, forever, forget, all of those that you may have learned from the documentation. So we can see if we put, yeah, notice it's going to serialize the value. And then it uses setX.
So we can see if we put, yeah, notice it's going to serialize the value. And then it uses setX. We haven't actually reviewed that yet. But when you do want your items to expire, and in fact, we haven't really talked about it at all, but Redis, of course, is a great usage for caching. So when you do want your cache items to expire, you can use setX. But anyways, it applies the prefix. It applies the key you specify. It determines when it will expire. And then it references the serialized value.
It determines when it will expire. And then it references the serialized value. And then finally, when you get it out, yeah, it's going to un-serialize the value and return it to you. So just something to be aware of. If you're wondering, well, I use Redis all the time, but I never have to reference the Redis facade, that's because behind the scenes, there is a Redis driver, and the API is normalized. But when you do need to dig a little bit lower, and you do need things like sorted sets, or you do have to work with hashes, and you just need a little more control, that's where you reference the Redis facade.
you do have to work with hashes, and you just need a little more control, that's where you reference the Redis facade. Okay, so we will continue in the next episode and review some more.
