1
00:00:02,280 --> 00:00:05,250
So in this session we are going to learn about Redis  sets.

2
00:00:05,840 --> 00:00:13,650
So sets are basically unordered collection of strings and it supports operation such as adding and removing

3
00:00:13,830 --> 00:00:18,540
or checking existence of the members which are a part of a set.

4
00:00:21,350 --> 00:00:23,190
if you talk about the feature of Sets.

5
00:00:23,210 --> 00:00:26,810
So basically Set doesn't allow us to repeat the members.

6
00:00:26,810 --> 00:00:32,470
So if you have same members stored in the set once, then you won't be allowed to store the same

7
00:00:32,470 --> 00:00:36,240
number again and about the use case.

8
00:00:36,240 --> 00:00:37,270
Okay.

9
00:00:37,410 --> 00:00:44,730
This you can use if you want to see like unique visitors of your Web site or if you want to store like

10
00:00:44,730 --> 00:00:45,450
top 5

11
00:00:46,110 --> 00:00:54,500
You know, top 5 winners, or those kind of use cases. You can use Sets. So let's begin with some example.

12
00:00:54,540 --> 00:00:59,440
So let me check if I already have something,  I already have some data stored.So

13
00:00:59,460 --> 00:01:08,390
I'm going to flush everything and now we are going to begin from the fresh. Okay.

14
00:01:08,400 --> 00:01:15,120
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

15
00:01:15,120 --> 00:01:16,290
sets.

16
00:01:16,350 --> 00:01:20,870
So you need to you sadd command and you need to provide a key name.

17
00:01:20,880 --> 00:01:30,430
So in this case, let's say we want to store all the visitors of our website. So I'll give the key name

18
00:01:30,430 --> 00:01:38,050
as user's IP and then we'll be storing IP addresses of the visitors. So to store one member, you can just

19
00:01:38,050 --> 00:01:43,990
type 1 IP and hit enter. If you want to store multiple members, you can provide multiple members

20
00:01:44,110 --> 00:01:45,340
separated by space

21
00:01:51,800 --> 00:01:53,410
similar to this one.

22
00:01:53,420 --> 00:01:58,850
So now if you want to see total number of members available in this list.

23
00:01:58,850 --> 00:02:02,390
So for that one we have a command which is called cardinality.

24
00:02:03,560 --> 00:02:08,690
So scard card is going to tell you, the total number of members available in this set.

25
00:02:09,590 --> 00:02:11,770
So let's run scard

26
00:02:11,900 --> 00:02:14,600
And then you need to provide the key name.

27
00:02:14,690 --> 00:02:20,040
Also you can see that Redis client is giving you a hint that you need to provide Key Name.

28
00:02:20,150 --> 00:02:28,190
So once we do that and hit enter. We can see that there are total 4 members available in this key.

29
00:02:28,190 --> 00:02:32,060
So one of the member we added here and then we added another 3 members here.

30
00:02:32,600 --> 00:02:35,960
So it is giving us the correct count.

31
00:02:36,020 --> 00:02:40,960
Now let's say we want to see a list of all the members which are stored in this set.

32
00:02:41,030 --> 00:02:45,590
So for that command is  smembers and you need to provide the key name.

33
00:02:48,700 --> 00:02:53,700
Hit enter and then you'll be able to see all the members, which are part of this key

34
00:02:57,920 --> 00:03:04,200
Now let's say if you want to see or you want to do kind of comparison between yesterday's visitors and

35
00:03:04,200 --> 00:03:05,100
today's visitor.

36
00:03:05,760 --> 00:03:10,850
So we have a command which is called s difference or s diff.

37
00:03:10,860 --> 00:03:16,760
Basically that's going to give you  a difference between members of two keys.

38
00:03:17,480 --> 00:03:19,470
So let me show you by example.

39
00:03:19,610 --> 00:03:26,270
So let's say we have users IP  which stores visitors of today and then we have another key which is called

40
00:03:27,320 --> 00:03:30,760
previous IP.

41
00:03:30,760 --> 00:03:36,820
This is the one which is going to store IP addresses of the members who visited previously or let's say who

42
00:03:36,820 --> 00:03:42,680
visited yesterday. So let's rather just make it Yesterday Visit.

43
00:03:47,580 --> 00:03:55,180
I'm just going to call it Yesterday IP and then let's say there was  user 10.0. 0.1

44
00:03:55,230 --> 00:04:00,950
So this is a visitor, who visited yesterday also and then we have another visitors which is

45
00:04:01,010 --> 00:04:04,090
192.168. 0

46
00:04:04,110 --> 00:04:13,130
This is another visitor who had visited yesterday and then we also had some more visitors like with the

47
00:04:13,150 --> 00:04:19,010
IP of this and I'm just going to add  couple of more's

48
00:04:23,920 --> 00:04:25,960
Hit enter and now let's see

49
00:04:25,960 --> 00:04:27,850
all the members in this key.

50
00:04:33,630 --> 00:04:33,930
okay.

51
00:04:33,940 --> 00:04:36,070
So these are our yesterday's visitors.

52
00:04:36,070 --> 00:04:39,450
And then these are the one's which are over today's visitor.

53
00:04:39,840 --> 00:04:46,630
And now if we want to see the difference between yesterday's visitors and today's visitor we can do

54
00:04:47,980 --> 00:04:49,110
sdiff

55
00:04:49,370 --> 00:05:00,170
So I'm going to do sdiff and then let's do a difference between users IP.

56
00:05:00,170 --> 00:05:03,280
This is the one which is keeping our today's visitor

57
00:05:03,470 --> 00:05:04,970
And then we have yesterday's IP.

58
00:05:04,970 --> 00:05:07,580
This is the one which is keeping yesterday's visitor.

59
00:05:07,820 --> 00:05:12,440
So once we hit enter. We are getting only 2 members.

60
00:05:12,460 --> 00:05:12,720
OK.

61
00:05:12,760 --> 00:05:21,120
So these are the members or you can say these are the members which were, which visited yesterday but

62
00:05:21,120 --> 00:05:24,060
did not visit today. Okay.

63
00:05:24,100 --> 00:05:29,770
Or if you want to see which  are the members which visited  today only and which had not visited yesterday.

64
00:05:29,770 --> 00:05:36,820
In that case you need to type sdiff and you need to provide yesterday's IP and then you need to

65
00:05:36,820 --> 00:05:39,540
provide today's.

66
00:05:39,790 --> 00:05:45,930
And now this is going to give you only the users, who have visited today but have not visited yesterday.

67
00:05:54,090 --> 00:06:00,760
Now let's say you want to see whether one of the member exists or already exist in a set or not.

68
00:06:00,810 --> 00:06:06,960
In that case, you use command which is called S is member.

69
00:06:07,290 --> 00:06:13,530
So we have sismember command which is going to give you, or going to check basically the existence

70
00:06:13,590 --> 00:06:21,500
of a specific member. So as we know, we have a key which is yesterday IP and we have added one of the

71
00:06:21,500 --> 00:06:23,750
members with this IP.

72
00:06:23,750 --> 00:06:30,470
So I'm just going to copy and paste that and then if wehit enter, we are going to get a reply of 1

73
00:06:30,470 --> 00:06:35,920
which means that this member is already part of this Set.

74
00:06:35,960 --> 00:06:39,530
Now let's say if I give some different IP which doesn't exist

75
00:06:44,070 --> 00:06:50,310
and if I hit enter this time I get a reply of 0, which means this member is not part of the Set.

76
00:06:53,400 --> 00:06:56,310
So you can use it for Use Cases, like if you want to see that

77
00:06:56,500 --> 00:07:00,560
this member already visited our website today or it doesn't visit.

78
00:07:00,750 --> 00:07:07,290
So you can use it for those kind of Use Cases and then you can show some promo to the user who hasn't

79
00:07:07,290 --> 00:07:10,770
visited or who has visited this website for the first time

80
00:07:15,440 --> 00:07:22,190
and now we are going to discuss about moving one of the members from one list or one Set to another

81
00:07:22,190 --> 00:07:22,460
Set.

82
00:07:26,390 --> 00:07:33,170
So the command for that is smove and now as soon as I type smove, you can see that it is giving me

83
00:07:33,190 --> 00:07:38,700
hint that I need to provide Source and Destination and also the Member which I want to move.

84
00:07:39,600 --> 00:07:48,440
So let's say I want to move from yesterday IP, I want to move to a new key which is called,

85
00:07:53,220 --> 00:07:57,890
let's call it previous IP.

86
00:07:58,170 --> 00:07:58,590
Okay.

87
00:07:58,590 --> 00:08:06,660
And we want to move a member which is having IP address of 192.168.0

88
00:08:06,670 --> 00:08:08,020
Now once we hit enter

89
00:08:12,230 --> 00:08:14,950
one sec and let me adjust this.

90
00:08:19,680 --> 00:08:20,280
okay.

91
00:08:20,330 --> 00:08:21,950
So now you can see.

92
00:08:21,950 --> 00:08:28,700
So once we hit enter we can see we got a response of 1, which means that it has moved this member from

93
00:08:29,830 --> 00:08:32,090
yesterday IP to previous IP key.

94
00:08:32,110 --> 00:08:37,570
And remember if the key doesn't exist. Redis is going to create that key.

95
00:08:37,570 --> 00:08:39,790
So  previous IP was not existing.

96
00:08:39,790 --> 00:08:47,910
So Redis created a new key and also assigned or moved this member from yesterday IP to previous IP.

97
00:08:48,040 --> 00:08:58,070
So let's see members of previous IP and now we can see we have this member added in this new key.

98
00:08:58,070 --> 00:09:04,460
Now let's say if the key only exists and if we want to move another member, then what's going to happen?

99
00:09:09,970 --> 00:09:15,070
So I'm going to move another member with IP of this

100
00:09:20,490 --> 00:09:22,980
and this is the Key where we want to move this member.

101
00:09:23,760 --> 00:09:24,030
Okay.

102
00:09:24,030 --> 00:09:26,940
So hit enter and you get a response of 1

103
00:09:26,970 --> 00:09:27,210
Okay.

104
00:09:27,210 --> 00:09:30,240
So the key was already existing.  Redis knew it.

105
00:09:30,300 --> 00:09:40,050
And so it has just added the member to that set.  So now if we do  smembers again then we are getting

106
00:09:40,050 --> 00:09:41,820
the response with 2 members

107
00:09:47,110 --> 00:09:56,590
and  now let's talk about removing one of the members from the Set.  So for removing,  we have two commands

108
00:09:56,590 --> 00:09:58,970
one command is s Pop.

109
00:09:58,990 --> 00:10:07,040
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

110
00:10:07,100 --> 00:10:13,680
some specific member, then we have a command which is called s remove and short form is srem.

111
00:10:13,700 --> 00:10:16,050
This is a command which you need to use.

112
00:10:16,070 --> 00:10:20,600
So let's clear the screen first and I'm going to add few members

113
00:10:24,220 --> 00:10:26,980
in our key, which is called Users.

114
00:10:26,980 --> 00:10:28,300
And let me add  few names

115
00:10:35,180 --> 00:10:36,630
and hit enter.

116
00:10:36,710 --> 00:10:39,380
Let's check the members by using smembers command.

117
00:10:43,260 --> 00:10:45,300
I just typed the commands incorrectly.

118
00:10:45,300 --> 00:10:52,380
So we have to type smembers and the Key Name, which is users and now we see all the members which are added

119
00:10:52,380 --> 00:10:52,530
here.

120
00:10:52,560 --> 00:10:57,100
So here if i do   spop, it is going to randomly remove one of the user.

121
00:11:03,750 --> 00:11:04,310
okay.

122
00:11:05,500 --> 00:11:07,330
And let's see s members again

123
00:11:11,640 --> 00:11:11,950
OK.

124
00:11:11,990 --> 00:11:20,320
So once we see this users Set again. Now we can see we have only 4 members which are remaining in

125
00:11:20,320 --> 00:11:28,610
this Set.  Now let's say rather than 1, if you want to remove 2 or 3 members or any number of members

126
00:11:28,670 --> 00:11:29,840
out of this list.

127
00:11:29,840 --> 00:11:35,510
So in that case you can use spop. So  guys, if I'm saying List,

128
00:11:35,510 --> 00:11:39,080
So basically I'm just talking about Set, in this session.

129
00:11:39,080 --> 00:11:48,010
So do not confuse it with List command, which is a separate you know, separate Data type. Okay, because by

130
00:11:48,010 --> 00:11:52,440
List, I just mean is Listing of these members. Okay.

131
00:11:52,450 --> 00:11:56,480
So basically in today's session, it is all about Sets and Sets Data Type.

132
00:11:56,550 --> 00:11:58,360
Okay.

133
00:11:58,390 --> 00:12:01,720
So, let's say I want to remove multiple members from this Sets.

134
00:12:01,750 --> 00:12:07,870
So in that case I need to use spop and I need to give the key name which is Users and then we need

135
00:12:07,870 --> 00:12:11,050
to give the Number of members which we want to remove.

136
00:12:11,050 --> 00:12:19,390
So in this case, if I give 2, it is going to remove 2 members from this set. OK.

137
00:12:19,400 --> 00:12:26,600
So it has removed these 2 members randomly and you cannot decide which is the member which you want

138
00:12:26,600 --> 00:12:27,050
to remove.

139
00:12:27,050 --> 00:12:32,980
So it's just going to remove 2 members randomly and now let's see the list again.

140
00:12:39,430 --> 00:12:40,010
OK.

141
00:12:40,160 --> 00:12:46,400
So now we can see, this is the, these are the members which we have in our users Set.

142
00:12:55,020 --> 00:13:02,540
and now I'm going to add  few more members again and notice I'm just copy pasting the same command,

143
00:13:02,540 --> 00:13:04,400
which we had added previously.

144
00:13:04,400 --> 00:13:10,820
So in this case, you can see we already have members like John and Martin in this set.

145
00:13:11,360 --> 00:13:16,540
However, once we try to Edit again basically it is just going to ignore these members.

146
00:13:16,610 --> 00:13:23,900
So let's hit enter and we see, we have 3 items added because 2 users name which are called John

147
00:13:23,900 --> 00:13:25,340
and Martin were already there.

148
00:13:25,370 --> 00:13:35,650
So it just added 3 more members and let's see the Sets by using  smembers.

149
00:13:35,810 --> 00:13:41,330
So now we can say these are the members which we have. Now let's say, if I want to remove some specific

150
00:13:41,330 --> 00:13:44,750
member. Let's say in this case, I want to remove John.

151
00:13:45,230 --> 00:13:48,980
So in that case, I need to use srem command.

152
00:13:48,980 --> 00:13:55,630
So this is shortform for Remove, and then you need to provide the Key Name and then you need to provide

153
00:13:55,630 --> 00:14:01,610
the Member Name. If you want to remove Multiple Members, you can provide multiple members you need to

154
00:14:01,610 --> 00:14:04,140
separate all the members by a space.

155
00:14:04,160 --> 00:14:08,690
So in this case let's remove 1 member and let's check the Sets  again.

156
00:14:11,800 --> 00:14:12,330
OK.

157
00:14:12,360 --> 00:14:15,270
And now let's remove couple of more members.

158
00:14:19,690 --> 00:14:22,440
Hit enter and now you can see,

159
00:14:25,110 --> 00:14:34,580
Set is having only 2 users left. So that's how you deal with Sets and you perform operations on Sets .

160
00:14:34,580 --> 00:14:40,250
And again remember whatever we are doing through commands, basically you are, you going to use a programming

161
00:14:40,250 --> 00:14:43,490
language in real world, to interact with Redis.

162
00:14:43,640 --> 00:14:48,560
So either you are going to use Python or Jython or any other programming language.

163
00:14:48,560 --> 00:14:55,700
And basically it is your application, which is going to be interacting with Redis. so all your logics are

164
00:14:55,700 --> 00:15:01,520
going to be defined in programming language and based on that language, you are going to make a choice

165
00:15:01,640 --> 00:15:07,130
on which command  you want to use and what is the  operations you want to perform.

166
00:15:07,250 --> 00:15:10,130
Again all of these will depend on the Use Case, which you have.
