Basic Tutorials of Redis(4) -Set
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Basic Tutorials of Redis(4) -Set,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4780字,纯文字阅读大概需要7分钟。
内容图文
![Basic Tutorials of Redis(4) -Set](/upload/InfoBanner/zyjiaocheng/1145/f5b9f4b6f1a546e4b372446be22ddd22.jpg)
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.
sadd set-111 sadd set-112131415
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 set-1
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 set-1 spop set-12
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 set-11113
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 set-111
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 set-1
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 set-1set-2 sunion set-1set-2 sdiff set-1set-2 sdiff set-2set-1
sinterstore inter-setset-1set-2 sunionstore union-setset-1set-2 sdiffstore diff-set-1set-1set-2 sdiffstore diff-set-2set-2set-1
After showing the native commands,we should turn to the usage of StackExchange.Redis.
// sadd smembers db.SetAdd("set-1", 11); var set_1 = new RedisValue[4] {12,13,14,15 }; db.SetAdd("set-1", set_1); Console.WriteLine("the members of the set-1 :"); foreach (var item in db.SetMembers("set-1")) { Console.WriteLine(item); } //spop srem Console.WriteLine(string.Format("the value was poped is {0}", db.SetPop("set-1"))); Console.WriteLine(string.Format("the value was poped is {0}", db.SetPop("set-1"))); Console.WriteLine(string.Format("the value was poped is {0}", db.SetPop("set-1"))); db.SetRemove("set-1", db.SetMembers("set-1")); Console.WriteLine(string.Format("amount of set-1 is {0}", db.SetMembers("set-1").Length)); //sismember Console.WriteLine(string.Format("11 {0} the member of set-1",db.SetContains("set-1",11)?"is":"isn‘t")); var set_1_again = new RedisValue[5] {11, 12, 13, 14, 15 }; db.SetAdd("set-1", set_1_again); Console.WriteLine(string.Format("11 {0} the member of set-1", db.SetContains("set-1", 11) ? "is" : "isn‘t")); //scard Console.WriteLine(string.Format("amount of set-1 is {0}", db.SetLength("set-1"))); var set_2 = new RedisValue[4] { 13, 14, 15,16 }; db.SetAdd("set-2", set_2); //sinter Console.WriteLine("the result of intersect:"); foreach (var item in db.SetCombine(SetOperation.Intersect, new RedisKey[2] { "set-1", "set-2" })) { Console.WriteLine(item); } // sunoin Console.WriteLine("the result of union:"); foreach (var item in db.SetCombine(SetOperation.Union, new RedisKey[2] { "set-1", "set-2" })) { Console.WriteLine(item); } //sdiff Console.WriteLine("the result of difference1:"); foreach (var item in db.SetCombine(SetOperation.Difference, new RedisKey[2] { "set-1", "set-2" })) { Console.WriteLine(item); } Console.WriteLine("the result of difference2:"); foreach (var item in db.SetCombine(SetOperation.Difference, new RedisKey[2] { "set-2", "set-1" })) { Console.WriteLine(item); }
原文:http://www.cnblogs.com/catcher1994/p/5843371.html
内容总结
以上是互联网集市为您收集整理的Basic Tutorials of Redis(4) -Set全部内容,希望文章能够帮你解决Basic Tutorials of Redis(4) -Set所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。