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-
sadd 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.

smembers set-

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

  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-  

  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- 

  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-

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

  We can store the result of the set opreation too.
sinterstore inter-set set- set-
sunionstore union-set set- set-
sdiffstore diff-set- set- set-
sdiffstore diff-set- set- set-

  After showing the native commands,we should turn to the usage of StackExchange.Redis.

             //sadd smembers
db.SetAdd("set-1", );
var set_1 = new RedisValue[] {,,, };
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",)?"is":"isn't"));
var set_1_again = new RedisValue[] {, , , , };
db.SetAdd("set-1", set_1_again);
Console.WriteLine(string.Format("11 {0} the member of set-1", db.SetContains("set-1", ) ? "is" : "isn't")); //scard
Console.WriteLine(string.Format("amount of set-1 is {0}", db.SetLength("set-1"))); var set_2 = new RedisValue[] { , , , };
db.SetAdd("set-2", set_2); //sinter
Console.WriteLine("the result of intersect:");
foreach (var item in db.SetCombine(SetOperation.Intersect, new RedisKey[] { "set-1", "set-2" }))
{
Console.WriteLine(item);
}
// sunoin
Console.WriteLine("the result of union:");
foreach (var item in db.SetCombine(SetOperation.Union, new RedisKey[] { "set-1", "set-2" }))
{
Console.WriteLine(item);
}
//sdiff
Console.WriteLine("the result of difference1:");
foreach (var item in db.SetCombine(SetOperation.Difference, new RedisKey[] { "set-1", "set-2" }))
{
Console.WriteLine(item);
} Console.WriteLine("the result of difference2:");
foreach (var item in db.SetCombine(SetOperation.Difference, new RedisKey[] { "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.

Basic Tutorials of Redis(4) -Set的更多相关文章

  1. Basic Tutorials of Redis(9) -First Edition RedisHelper

    After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...

  2. Basic Tutorials of Redis(2) - String

    This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...

  3. Basic Tutorials of Redis(8) -Transaction

    Data play an important part in our project,how can we ensure correctness of the data and prevent the ...

  4. Basic Tutorials of Redis(7) -Publish and Subscribe

    This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...

  5. Basic Tutorials of Redis(6) - List

    Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...

  6. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  7. Basic Tutorials of Redis(3) -Hash

    When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...

  8. Basic Tutorials of Redis(1) - Install And Configure Redis

    Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...

  9. 【转】Redis入门

    Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...

随机推荐

  1. 如何利用pt-online-schema-change进行MySQL表的主键变更

    业务运行一段时间,发现原来的主键设置并不合理,这个时候,想变更主键.这种需求在实际生产中还是蛮多的. 下面,看看pt-online-schema-change解决这类问题的处理方式. 首先,创建一张测 ...

  2. 简约之美Jodd-http--深入源码理解http协议

    Jodd 是一个开源的 Java 工具集, 包含一些实用的工具类和小型框架.简单,却很强大! jodd-http是一个轻巧的HTTP客户端.现在我们以一个简单的示例从源码层看看是如何实现的? Http ...

  3. 搭建属于自己的VIP积分系统(1)

    很久没写博客了,如果有写得不好的地方,还请多多见谅. 架构设计 需求分析 这篇文章主要是介绍此VIP系统的基础架构.说实在的,我其实对 架构方面也不是很懂,我这套框架 还是拿别人的东西改过来的,并不是 ...

  4. java时间

    Calendar.getInstance().getTime() 获取当前时间(包括星期和时区 CST China Standard Time):  Fri Jan 06 21:03:36 CST 2 ...

  5. Java程序:从命令行接收多个数字,求和并输出结果

    一.设计思想:由于命令行接收的是字符串类型,因此应先将字符串类型转化为整型或其他字符型,然后利用for循环求和并输出结果 二.程序流程图: 三.源程序代码:   //王荣荣 2016/9/23     ...

  6. JavaScript 正则表达式语法

    定义 JavaScript定义正则表达式有两种方法. 1.RegExp构造函数 var pattern = new RegExp("[bc]at","i"); ...

  7. SAP CRM 性能小技巧

    导言 本页面打算收集SAP CRM实施中可以用于避免性能问题的注意事项,重要的事项会由图标标识. 如果你有其他的技巧想要说出来,别犹豫! 性能注意事项 通用 缓存读取类访问,特别是在性能关键的地方,比 ...

  8. sqlyog导出json数据格式支持mysql数据转存mongodb

    <!-------------知识的力量是无限的(当然肯定还有更简单的方法)-----------!> 当我考虑将省市区三级联动数据从mysql转入mongodb时遇到了网上无直接插入mo ...

  9. 什么是英特尔® Edison 模块?

    英特尔® Edison 模块 是一种 SD 卡大小的微型计算芯片,专为构建物联网 (IoT) 和可穿戴计算产品而设计. Edison 模块内含一个高速的双核处理单元.集成 Wi-Fi*.蓝牙* 低能耗 ...

  10. DDD中的Unitwork与DomainEvent如何相容?(续)

    上篇中说到了面临的问题(传送门:DDD设计中的Unitwork与DomainEvent如何相容?),和当时实现的一个解决方案.在实际使用了几天后,有了新的思路,和@trunks 兄提出的观点类似.下面 ...