Basic Tutorials of Redis(4) -Set

  This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it 

data was stored in the database randomly.And there are 15 commands you can use in Redis,the same as Hash.

  

  For storing the data to database,we can use the command sadd to finish the job.The sadd is very powerful,we

can not only use it to add a single value to the key,but also multi values.For example,I add 11 to the key named

set-1 at first.Laterly I add 12 ~15 too.So easy the command is.When you execute the sadd command,the client

will return the amount of the set.

sadd - -

   After executing the command sadd,it will return a integer to show us the amount of this set.But what can we

know the members of this set?We can use smembers to get all of the members in the set.

smembers -

  There are two commands to help us to remove the members of the set.The spop 

member of the set randomly.As the following picture,I remove a member from the set-1 firstly,and then I remove two

members from the set-1.At last,we will find that the set-1 only has 11 and 13.

spop --

  srem,the second command of removing members from the set,can remove the members 

ideas,not randomly.I removed the last two members from the set-1 by this command.At this time,I want to get all of the

members of the set-1,you will get the information that the set is empty.

srem -

  When we are coding,the most things we do is to judge a member whether exists in the 

thing as well.The set-1 is empty now,I judge the member 11 whether exists in the set-11,it returns 0 meaning 11 is not

the member of the set.After adding members to this set,the second time to judge returns 1 meaning 11 is the member of set-1.

sismember -

   As all you know,we use the Property length or the method count to get how many members in the array by using C#.

In Redis,we get the numbers of members in a set by using scard.

scard -

   The commands I will show you next needs at lease two sets,so I have to add another one.And you will be familiar with

the set opreation of Mathematical.Command sinter will return the command members both set-1 and set-2 contain.Command

sunion will return all of the members both set-1 and set-2 contian.Command sdiff will return the difference members from the sets.

sinter - -- -- -- -

  We can store the result of the set opreation too.
sinterstore inter- - -- - --- - --- - -

   After showing the native commands,we should turn to the usage of StackExchange.Redis.

db.SetAdd(, set_1 = RedisValue[] {,,, ( item db.SetMembers( </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;spop srem</span> Console.WriteLine(<span style="color: #0000ff;"&gt;string</span>.Format(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;the value was poped is {0}</span><span style="color: #800000;"&gt;"</span>,db.SetPop(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;))); Console.WriteLine(</span><span style="color: #0000ff;"&gt;string</span>.Format(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;the value was poped is {0}</span><span style="color: #800000;"&gt;"</span>,db.SetPop(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;))); db.SetRemove(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span>,db.SetMembers(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)); Console.WriteLine(</span><span style="color: #0000ff;"&gt;string</span>.Format(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;amount of set-1 is {0}</span><span style="color: #800000;"&gt;"</span>,db.SetMembers(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;).Length)); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;sismember</span> Console.WriteLine(<span style="color: #0000ff;"&gt;string</span>.Format(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;11 {0} the member of set-1</span><span style="color: #800000;"&gt;"</span>,db.SetContains(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800080;"&gt;11</span>)?<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;is</span><span style="color: #800000;"&gt;"</span>:<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;isn't</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)); </span><span style="color: #0000ff;"&gt;var</span> set_1_again = <span style="color: #0000ff;"&gt;new</span> RedisValue[<span style="color: #800080;"&gt;5</span>] {<span style="color: #800080;"&gt;11</span>,<span style="color: #800080;"&gt;12</span>,set_1_again); Console.WriteLine(</span><span style="color: #0000ff;"&gt;string</span>.Format(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;11 {0} the member of set-1</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800080;"&gt;11</span>) ? <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;is</span><span style="color: #800000;"&gt;"</span> : <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;isn't</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;scard</span> Console.WriteLine(<span style="color: #0000ff;"&gt;string</span>.Format(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;amount of set-1 is {0}</span><span style="color: #800000;"&gt;"</span>,db.SetLength(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;))); </span><span style="color: #0000ff;"&gt;var</span> set_2 = <span style="color: #0000ff;"&gt;new</span> RedisValue[<span style="color: #800080;"&gt;4</span>] { <span style="color: #800080;"&gt;13</span>,<span style="color: #800080;"&gt;15</span>,<span style="color: #800080;"&gt;16</span><span style="color: #000000;"&gt; }; db.SetAdd(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-2</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,set_2); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;sinter</span> Console.WriteLine(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;the result of intersect:</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;); </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span> db.SetCombine(SetOperation.Intersect,<span style="color: #0000ff;"&gt;new</span> RedisKey[<span style="color: #800080;"&gt;2</span>] { <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-2</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt; })) { Console.WriteLine(item); } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; sunoin </span> Console.WriteLine(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;the result of union:</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;); </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span> db.SetCombine(SetOperation.Union,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-2</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt; })) { Console.WriteLine(item); } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;sdiff</span> Console.WriteLine(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;the result of difference1:</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;); </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span> db.SetCombine(SetOperation.Difference,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-2</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt; })) { Console.WriteLine(item); } Console.WriteLine(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;the result of difference2:</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;); </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span> db.SetCombine(SetOperation.Difference,<span style="color: #0000ff;"&gt;new</span> RedisKey[<span style="color: #800080;"&gt;2</span>] { <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-2</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;set-1</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt; })) { Console.WriteLine(item); } </span></pre>
  When you debug the codes,the results are as follow.

  

  The next post of this series is the basic opreation of Sorted Set in Redis.

相关文章

在笔者近 3 年的 Java 一线开发经历中,尤其是一些移动端、用...
这一篇文章拖了有点久,虽然在项目中使用分布式锁的频率比较...
本文梳理总结了一些 Java 互联网项目中常见的 Redis 缓存应用...
书接上回,消息通知系统(notification-system)作为一个独立...
Redis 是目前互联网后端的热门中间件之一,在许多方面都有深...
在Java Spring 项目中,数据与远程数据库的频繁交互对服务器...