This episode is for members only

Sign up to access "Liking Things in Laravel with Redis" right now.

Get started
Already a member? Sign in to continue
Playing
03. Let’s add a like with Redis

Transcript

00:00
OK, so let's add the ability to like one of the comments
00:03
that we have. Just before we do this, let's go over some of the basics of using sets within Redis. So again, we're going to boot into our Redis CLI.
00:13
And before we do anything, we'll hop over to the command documentation, which is incredibly useful. So pretty much every set command starts with S. And we want to add something to a set.
00:25
So let's go ahead and click on this and just read a little bit more about it. So we've got a key and a member. Now, think of a set kind of like an array
00:33
that we can add things to. Let's just play around with this over in our terminal. So we're going to go ahead and say S, Add. We're going to set the key to something
00:41
that relates to our comments. So I'm going to say comments.1 and .likes. We might have something else under this kind of namespace. Now, what we can do is we can add the ID of the user who
00:53
has liked this thing. So I'm going to add the user ID 1 to comments.1.likes as the key name. So let's go ahead and add that in there now.
01:03
OK, so the next thing that we could do is go ahead and pull this stuff out. So we could say SCard, which gets the cardinality of what we have just stored,
01:13
which is just the count. And as you can see, we get 1. Let's try that second one again and add another user to this and then do exactly the same thing.
01:22
And you can see that we get two likes returned. So how are we going to do this within Laravel? Well, we are going to, first of all, hop over to the comment model and put everything in here.
01:32
And then, like we saw from the introduction, move this over to a trait. So let's create a method on here called AddLike, which will, of course, add a like.
01:42
And we're going to do this for a specific user. So let's pull the user directly into here because we obviously need their ID. Now, in Laravel, we have a Redis facade.
01:51
So if we just pull this in, any method that we supply statically to this will map directly up with Redis. So as we've just seen, we've used the SAdd command directly
02:03
within Redis. And this will work in exactly the same way. It will connect up to Redis. And it will use SAdd.
02:10
Now, the first thing here is the key. So let's just go ahead and scaffold this out and say likes.to.likes or comments.to.likes. And then we'll find this directly within Redis.
02:21
The second thing that we want to pass in are the arguments. So in this case, it will be the user's ID. OK, before we go anywhere, let's just switch out the ID of this.
02:30
And then we'll see a slightly better way that we can do this. So let's concatenate on the ID of the current thing, which is just our comment. So if we're dealing with comment 1,
02:40
this will be comments.1.likes. Same thing with 2. OK, so now that we've got the ability to add a like, we want to create some sort of route
02:47
or, if you're using something like LiveWire, a method that will actually call add like on the comment that we're currently working within. So we're going to do this over on the web routes
02:57
just to keep things really, really simple. Then, of course, you can translate this to any of your own application structures. So we're going to say comments and then grab the comment
03:07
with root model binding. And we're going to say likes. And we're going to post a like to this. So let's go ahead and get rid of this view
03:13
because we're not going to need this. Let's make sure we pull the comment in here with root model binding. And then really in here, all we want to do
03:20
is say comment add like. And then we just want to pass the currently authenticated user through to this. And we'll probably want to return back
03:29
or something like that. So now we can head directly over to our comments index. And we can hook this up to this form and then check that this data has been stored.
03:37
So the action here is going to be a route. Now, we don't have a route name for this, but let's call this comments.likes.store. And of course, we want to pass the comment through to that
03:47
so it gets hooked up with root model binding. The method here is going to be post. And we'll need to include things like our cross-site request forgery protection as well.
03:56
So this should work really nicely as long as we make sure we name that route. So let's go over and just give this a name really quickly. Pop that in there, and we should be good.
04:05
Okay, so let's head over to the browser and just check this out. So when I click like, there we go. That should have liked that.
04:12
And it should now be returned back. Okay, so if we head back over to our terminal, let's go and boot into Redis CLI again. And what we're going to do is just say keys and everything.
04:24
That's just going to give us all of the keys that we have inside of here. And you can see that we've got comments.1.likes in there, which we created earlier.
04:33
And if we take a closer look, we've got this prefix Laravel database comment1likes. That's just how Laravel has inserted that. We don't really need to worry specifically
04:43
about how this data goes in because we can always fetch it back out with the Redis facade. So if we go down here
04:50
and we just say something like scard on that key, yeah, sure enough, we've got one. So that has liked that comment successfully. And all we need to do is figure out how to get this back.
05:01
So while we're here, let's go ahead and create another method over on our comment model, which will get the amount of likes for this.
05:08
So let's go and create a getLikeCount method here. Again, we don't need our user for this because this is just a general method. We're going to go ahead and use the Redis facade.
05:18
And sure enough, we're going to use scard, which we've already seen. And we're going to go and take this just here, paste this directly into here
05:26
because it's going to be exactly the same key and we should be good. So now we can use this getLikeCount method over on here to display how many likes we have.
05:36
So let's just directly output this in here using comment and getLikesCount or likeCount. And we should now be displaying that. So let's head over and give that a refresh.
05:47
And yeah, there we go. We've got one comment liked. Now notice that by default with a set in Redis, these are unique things.
05:56
So you're always going to have unique user IDs in your application. That means that when I try and add this again, it just doesn't work by default.
06:04
We could create a method to protect against this and create some sort of if statement in here if we've already liked something. But if we're using Redis set,
06:14
it's just going to work in that way anyway. So you can see that I can like every single one of these and these are now liked. Okay, so we're kind of stuck
06:21
with not being able to unlike things. So let's jump over to the next episode and look at how we can remove these items from this set and therefore remove likes.
6 episodes 21 mins

Overview

Let’s skip the database and build the ability to like any model in Laravel, using Redis.

Traditionally you’d reach for the database for this kind of thing, but as you load more models and start performing checks within relationships — things begin to slow down.

With a key-value store like Redis, tracking users who have liked comments (or anything) keeps everything ridiculously fast.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.