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. 解决cookie跨域访问

    一.前言 随着项目模块越来越多,很多模块现在都是独立部署.模块之间的交流有时可能会通过cookie来完成.比如说门户和应用,分别部署在不同的机器或者web容器中,假如用户登陆之后会在浏览器客户端写入c ...

  2. [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性

    回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都尚未进入正式阶段.C# 6.0 ...

  3. [.NET] C# 知识回顾 - Event 事件

    C# 知识回顾 - Event 事件 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6060297.html 序 昨天,通过<C# 知识回顾 - ...

  4. javascript运动系列第一篇——匀速运动

    × 目录 [1]简单运动 [2]定时器管理 [3]分享到效果[4]移入移出[5]运动函数[6]透明度[7]多值[8]多物体[9]回调[10]函数完善[11]最终函数 前面的话 除了拖拽以外,运动也是j ...

  5. 使用Microsoft的IoC框架:Unity来对.NET应用进行解耦

    1.IoC/DI简介 IoC 即 Inversion of Control,DI 即 Dependency Injection,前一个中文含义为控制反转,后一个译为依赖注入,可以理解成一种编程模式,详 ...

  6. required

    required,这是HTML5中的一个新属性:这是HTML5中input元素中的一个属性. required译为必须的,在input元素中应用这一属性,就表示这一input元素节点是必填的或者必选的 ...

  7. JavaScript学习笔记(二)——闭包、IIFE、apply、函数与对象

    一.闭包(Closure) 1.1.闭包相关的问题 请在页面中放10个div,每个div中放入字母a-j,当点击每一个div时显示索引号,如第1个div显示0,第10个显示9:方法:找到所有的div, ...

  8. iOS系列教程 目录 (持续更新...)

      前言: 听说搞iOS的都是高富帅,身边妹子无数.咱也来玩玩.哈哈. 本篇所有内容使用的是XCode工具.Swift语言进行开发. 我现在也是学习阶段,每一篇内容都是经过自己实际编写完一遍之后,发现 ...

  9. php利用root权限执行shell脚本

    vi /etc/sudoers , 为apache用户赋予root权限,并且不需要密码,还有一步重要的修改(我被困扰的就是这个地方) root  ALL=(ALL)  ALL apache  ALL= ...

  10. photoshop:无法完成请求 因为暂存盘已满

    今天photoshop打开一个问题,提醒:无法完成请求因为暂存盘已满 不用担心这个问题很好解决可能是你做的图比较大并不需要清理C盘空间 选择:编辑→首选项→暂存盘 设置第一暂存盘为D盘或E盘 总之 第 ...