博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Basic Tutorials of Redis(4) -Set
阅读量:7038 次
发布时间:2019-06-28

本文共 5339 字,大约阅读时间需要 17 分钟。

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

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 set-1 11sadd set-1 12 13 14 15

   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

  There are two commands to help us to remove the members of the set.The
spop 
command will remove a or multi

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-1spop set-1 2

 

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

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-1 11 13

 

  When we are coding , the most things we do is to judge a member whether exists in the 
set.In Redis,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 set-1 11

   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-1 set-2sunion set-1 set-2sdiff set-1 set-2sdiff set-2 set-1

 

  We can store the result of the set opreation too.
sinterstore inter-set set-1 set-2sunionstore union-set set-1 set-2sdiffstore diff-set-1 set-1 set-2sdiffstore diff-set-2 set-2 set-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); }
  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.

转载于:https://www.cnblogs.com/catcher1994/p/5843371.html

你可能感兴趣的文章
第5章 配置邮箱服务
查看>>
node.js的一个简单框架
查看>>
PPT如何保存还原已剪裁图片的原始版本
查看>>
lnmp一键安装之-php
查看>>
ajax 同步和异步的区别
查看>>
linux shell单引号、双引号及无引号区别(考试题答案系列)--看到这篇文章之后我豁然开朗...
查看>>
排错 zabbix-agent 主机重启无法被监控
查看>>
win10操作系统
查看>>
Mutual Funds引起的一桩桩血案
查看>>
zabbix如何监控nginx性能
查看>>
python3的异常处理
查看>>
linux C 动态共享库编译链接
查看>>
用jdbcTempate调用存储过程,处理BLOBCLOB小记
查看>>
oracle study road
查看>>
《你必须知道的495个C语言问题》笔记
查看>>
数据中心可靠级别划分
查看>>
你真的理解什么是“财富自由”吗?
查看>>
释放LINUX内存(请使用火狐浏览器浏览本页面)
查看>>
Andrew Ng 深度学习笔记-01-week3-课程
查看>>
Android获取通过XML设置的空间的高宽
查看>>