data was stored in the database randomly.And there are 15 commands you can use in Redis,the same as Hash.
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.
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.
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.
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.
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.
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.
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.
After showing the native commands,we should turn to the usage of StackExchange.Redis.
</span><span style="color: #008000;">//</span><span style="color: #008000;">spop srem</span>
Console.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">the value was poped is {0}</span><span style="color: #800000;">"</span>,db.SetPop(<span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span><span style="color: #000000;">)));
Console.WriteLine(</span><span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">the value was poped is {0}</span><span style="color: #800000;">"</span>,db.SetPop(<span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span><span style="color: #000000;">)));
db.SetRemove(</span><span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span>,db.SetMembers(<span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span><span style="color: #000000;">));
Console.WriteLine(</span><span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">amount of set-1 is {0}</span><span style="color: #800000;">"</span>,db.SetMembers(<span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span><span style="color: #000000;">).Length));
</span><span style="color: #008000;">//</span><span style="color: #008000;">sismember</span>
Console.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">11 {0} the member of set-1</span><span style="color: #800000;">"</span>,db.SetContains(<span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span>,<span style="color: #800080;">11</span>)?<span style="color: #800000;">"</span><span style="color: #800000;">is</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">isn't</span><span style="color: #800000;">"</span><span style="color: #000000;">));
</span><span style="color: #0000ff;">var</span> set_1_again = <span style="color: #0000ff;">new</span> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Value[<span style="color: #800080;">5</span>] {<span style="color: #800080;">11</span>,<span style="color: #800080;">12</span>,set_1_again);
Console.WriteLine(</span><span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">11 {0} the member of set-1</span><span style="color: #800000;">"</span>,<span style="color: #800080;">11</span>) ? <span style="color: #800000;">"</span><span style="color: #800000;">is</span><span style="color: #800000;">"</span> : <span style="color: #800000;">"</span><span style="color: #800000;">isn't</span><span style="color: #800000;">"</span><span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">scard</span>
Console.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">amount of set-1 is {0}</span><span style="color: #800000;">"</span>,db.SetLength(<span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span><span style="color: #000000;">)));
</span><span style="color: #0000ff;">var</span> set_2 = <span style="color: #0000ff;">new</span> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Value[<span style="color: #800080;">4</span>] { <span style="color: #800080;">13</span>,<span style="color: #800080;">15</span>,<span style="color: #800080;">16</span><span style="color: #000000;"> };
db.SetAdd(</span><span style="color: #800000;">"</span><span style="color: #800000;">set-2</span><span style="color: #800000;">"</span><span style="color: #000000;">,set_2);
</span><span style="color: #008000;">//</span><span style="color: #008000;">sinter</span>
Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">the result of intersect:</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">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;">new</span> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Key[<span style="color: #800080;">2</span>] { <span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">set-2</span><span style="color: #800000;">"</span><span style="color: #000000;"> }))
{
Console.WriteLine(item);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> sunoin </span>
Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">the result of union:</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">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;">"</span><span style="color: #800000;">set-2</span><span style="color: #800000;">"</span><span style="color: #000000;"> }))
{
Console.WriteLine(item);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">sdiff</span>
Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">the result of difference1:</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">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;">"</span><span style="color: #800000;">set-2</span><span style="color: #800000;">"</span><span style="color: #000000;"> }))
{
Console.WriteLine(item);
}
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">the result of difference2:</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">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;">new</span> Re<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>Key[<span style="color: #800080;">2</span>] { <span style="color: #800000;">"</span><span style="color: #800000;">set-2</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">set-1</span><span style="color: #800000;">"</span><span style="color: #000000;"> }))
{
Console.WriteLine(item);
}
</span></pre>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。