微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 dis,you can do this

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> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Value[<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> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Value[<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(S<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>peration.Intersect,<span style="color: #0000ff;"&gt;new</span> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Key[<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(S<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>peration.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(S<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>peration.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(S<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>peration.Difference,<span style="color: #0000ff;"&gt;new</span> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Key[<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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐