So in this session we are going to learn about Redis sets.
So sets are basically unordered collection of strings and it supports operation such as adding and removing
or checking existence of the members which are a part of a set.
if you talk about the feature of Sets.
So basically Set doesn't allow us to repeat the members.
So if you have same members stored in the set once, then you won't be allowed to store the same
number again and about the use case.
Okay.
This you can use if you want to see like unique visitors of your Web site or if you want to store like
top 5
You know, top 5 winners, or those kind of use cases. You can use Sets. So let's begin with some example.
So let me check if I already have something, I already have some data stored.So
I'm going to flush everything and now we are going to begin from the fresh. Okay.
So first thing I'm going to tell you how we can add the member of or how we can add a member in the
sets.
So you need to you sadd command and you need to provide a key name.
So in this case, let's say we want to store all the visitors of our website. So I'll give the key name
as user's IP and then we'll be storing IP addresses of the visitors. So to store one member, you can just
type 1 IP and hit enter. If you want to store multiple members, you can provide multiple members
separated by space
similar to this one.
So now if you want to see total number of members available in this list.
So for that one we have a command which is called cardinality.
So scard card is going to tell you, the total number of members available in this set.
So let's run scard
And then you need to provide the key name.
Also you can see that Redis client is giving you a hint that you need to provide Key Name.
So once we do that and hit enter. We can see that there are total 4 members available in this key.
So one of the member we added here and then we added another 3 members here.
So it is giving us the correct count.
Now let's say we want to see a list of all the members which are stored in this set.
So for that command is smembers and you need to provide the key name.
Hit enter and then you'll be able to see all the members, which are part of this key
Now let's say if you want to see or you want to do kind of comparison between yesterday's visitors and
today's visitor.
So we have a command which is called s difference or s diff.
Basically that's going to give you a difference between members of two keys.
So let me show you by example.
So let's say we have users IP which stores visitors of today and then we have another key which is called
previous IP.
This is the one which is going to store IP addresses of the members who visited previously or let's say who
visited yesterday. So let's rather just make it Yesterday Visit.
I'm just going to call it Yesterday IP and then let's say there was user 10.0. 0.1
So this is a visitor, who visited yesterday also and then we have another visitors which is
192.168. 0
This is another visitor who had visited yesterday and then we also had some more visitors like with the
IP of this and I'm just going to add couple of more's
Hit enter and now let's see
all the members in this key.
okay.
So these are our yesterday's visitors.
And then these are the one's which are over today's visitor.
And now if we want to see the difference between yesterday's visitors and today's visitor we can do
sdiff
So I'm going to do sdiff and then let's do a difference between users IP.
This is the one which is keeping our today's visitor
And then we have yesterday's IP.
This is the one which is keeping yesterday's visitor.
So once we hit enter. We are getting only 2 members.
OK.
So these are the members or you can say these are the members which were, which visited yesterday but
did not visit today. Okay.
Or if you want to see which are the members which visited today only and which had not visited yesterday.
In that case you need to type sdiff and you need to provide yesterday's IP and then you need to
provide today's.
And now this is going to give you only the users, who have visited today but have not visited yesterday.
Now let's say you want to see whether one of the member exists or already exist in a set or not.
In that case, you use command which is called S is member.
So we have sismember command which is going to give you, or going to check basically the existence
of a specific member. So as we know, we have a key which is yesterday IP and we have added one of the
members with this IP.
So I'm just going to copy and paste that and then if wehit enter, we are going to get a reply of 1
which means that this member is already part of this Set.
Now let's say if I give some different IP which doesn't exist
and if I hit enter this time I get a reply of 0, which means this member is not part of the Set.
So you can use it for Use Cases, like if you want to see that
this member already visited our website today or it doesn't visit.
So you can use it for those kind of Use Cases and then you can show some promo to the user who hasn't
visited or who has visited this website for the first time
and now we are going to discuss about moving one of the members from one list or one Set to another
Set.
So the command for that is smove and now as soon as I type smove, you can see that it is giving me
hint that I need to provide Source and Destination and also the Member which I want to move.
So let's say I want to move from yesterday IP, I want to move to a new key which is called,
let's call it previous IP.
Okay.
And we want to move a member which is having IP address of 192.168.0
Now once we hit enter
one sec and let me adjust this.
okay.
So now you can see.
So once we hit enter we can see we got a response of 1, which means that it has moved this member from
yesterday IP to previous IP key.
And remember if the key doesn't exist. Redis is going to create that key.
So previous IP was not existing.
So Redis created a new key and also assigned or moved this member from yesterday IP to previous IP.
So let's see members of previous IP and now we can see we have this member added in this new key.
Now let's say if the key only exists and if we want to move another member, then what's going to happen?
So I'm going to move another member with IP of this
and this is the Key where we want to move this member.
Okay.
So hit enter and you get a response of 1
Okay.
So the key was already existing. Redis knew it.
And so it has just added the member to that set. So now if we do smembers again then we are getting
the response with 2 members
and now let's talk about removing one of the members from the Set. So for removing, we have two commands
one command is s Pop.
So what s Pop is going to do is, it is going to remove one of the random members and if you want to remove
some specific member, then we have a command which is called s remove and short form is srem.
This is a command which you need to use.
So let's clear the screen first and I'm going to add few members
in our key, which is called Users.
And let me add few names
and hit enter.
Let's check the members by using smembers command.
I just typed the commands incorrectly.
So we have to type smembers and the Key Name, which is users and now we see all the members which are added
here.
So here if i do spop, it is going to randomly remove one of the user.
okay.
And let's see s members again
OK.
So once we see this users Set again. Now we can see we have only 4 members which are remaining in
this Set. Now let's say rather than 1, if you want to remove 2 or 3 members or any number of members
out of this list.
So in that case you can use spop. So guys, if I'm saying List,
So basically I'm just talking about Set, in this session.
So do not confuse it with List command, which is a separate you know, separate Data type. Okay, because by
List, I just mean is Listing of these members. Okay.
So basically in today's session, it is all about Sets and Sets Data Type.
Okay.
So, let's say I want to remove multiple members from this Sets.
So in that case I need to use spop and I need to give the key name which is Users and then we need
to give the Number of members which we want to remove.
So in this case, if I give 2, it is going to remove 2 members from this set. OK.
So it has removed these 2 members randomly and you cannot decide which is the member which you want
to remove.
So it's just going to remove 2 members randomly and now let's see the list again.
OK.
So now we can see, this is the, these are the members which we have in our users Set.
and now I'm going to add few more members again and notice I'm just copy pasting the same command,
which we had added previously.
So in this case, you can see we already have members like John and Martin in this set.
However, once we try to Edit again basically it is just going to ignore these members.
So let's hit enter and we see, we have 3 items added because 2 users name which are called John
and Martin were already there.
So it just added 3 more members and let's see the Sets by using smembers.
So now we can say these are the members which we have. Now let's say, if I want to remove some specific
member. Let's say in this case, I want to remove John.
So in that case, I need to use srem command.
So this is shortform for Remove, and then you need to provide the Key Name and then you need to provide
the Member Name. If you want to remove Multiple Members, you can provide multiple members you need to
separate all the members by a space.
So in this case let's remove 1 member and let's check the Sets again.
OK.
And now let's remove couple of more members.
Hit enter and now you can see,
Set is having only 2 users left. So that's how you deal with Sets and you perform operations on Sets .
And again remember whatever we are doing through commands, basically you are, you going to use a programming
language in real world, to interact with Redis.
So either you are going to use Python or Jython or any other programming language.
And basically it is your application, which is going to be interacting with Redis. so all your logics are
going to be defined in programming language and based on that language, you are going to make a choice
on which command you want to use and what is the operations you want to perform.
Again all of these will depend on the Use Case, which you have.