加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Basic Tutorials of Redis(4) -Set

发布时间:2020-12-16 04:46:54 所属栏目:安全 来源:网络整理
导读: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 us
  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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读