Basic Tutorials of Redis (4)-set

Source: Internet
Author: User

This post would introduce some usages of set in redis.the set is a unordered set,itmeans that the

Data is stored in the database randomly. And there is commands you can use in redis,the same as Hash.

  

For storing the data to Database,we can use the command SaddTo finish the job. The Saddis very powerful,we

Can not be use it to add a single value to the key,but also multi values. For example,i add one to the key named

Set-1 at first. Laterly I add ~15 too. The command is. When you execute the sadd command, the client

Would return the amount of the set.

set-1Set-1

After executing the command Sadd, it would return a integer to show us the amount of this set. But what can we

Know the members of this set? We can use a smembers to get all of the members in the set.

set-1

There is commands to help us to remove the members of the set. The Spop command would 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 II

Members from the set-1. At Last,we'll find that the set-1 only have has and 13.

set-1Set-12

   SremThe second command of removing members from the Set,can remove theFrom the set by your own

Ideas,not randomly. I removed the last of the last of the set-1 by this command. At this time,i want to get all of the

The set-1, you'll get the information that's the set is empty.

set-1

When we were coding, the most things we do be to judge a member whether exists in theset. In Redis,you can does this

Thing as well. The set-1 is empty Now,i judge the member one whether exists in the Set-11,it returns 0 meaning

The member of the set. After adding members to this set,the second time to judge returns 1 meaning are the member of Set-1.

set-1

As all, 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.

set-1

The commands I'll show you next needs at Lease II Sets,so I have to add another one. And you'll be familiar with

The set opreation of Mathematical.command sinter would return the Command members both Set-1 and set-2 contain. Command

Sunion 'll return all of the members both Set-1 and Set-2 Contian. Command Sdiff would return the difference members from the sets.

Set-1set-2Set-1set-2  Set-1set-2set-2set- 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 smembersDb. Setadd ("set-1", One); varSet_1 =Newredisvalue[4] { A, -, -, the }; Db. Setadd ("set-1", set_1); Console.WriteLine ("The members of the set-1:"); foreach(varIteminchDb. Setmembers ("set-1") {Console.WriteLine (item); }            //Spop SremConsole.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)); //SismemberConsole.WriteLine (string. Format ("{0} The member of Set-1"Db. Setcontains ("set-1", One)?" is":"isn ' t")); varSet_1_again =Newredisvalue[5] { One, A, -, -, the }; Db. Setadd ("set-1", Set_1_again); Console.WriteLine (string. Format ("{0} The member of Set-1"Db. Setcontains ("set-1", One) ?" is":"isn ' t")); //SCardConsole.WriteLine (string. Format ("amount of Set-1 is {0}"Db. SetLength ("set-1"))); varSet_2 =Newredisvalue[4] { -, -, the, - }; Db. Setadd ("set-2", set_2); //sinterConsole.WriteLine ("The result of intersect:"); foreach(varIteminchDb. Setcombine (Setoperation.intersect,Newrediskey[2] {"set-1","set-2"}) {Console.WriteLine (item); }            //SunoinConsole.WriteLine ("The result of union:"); foreach(varIteminchDb. Setcombine (Setoperation.union,Newrediskey[2] {"set-1","set-2"}) {Console.WriteLine (item); }            //SdiffConsole.WriteLine ("The result of Difference1:"); foreach(varIteminchDb. Setcombine (Setoperation.difference,Newrediskey[2] {"set-1","set-2"}) {Console.WriteLine (item); } Console.WriteLine ("The result of Difference2:"); foreach(varIteminchDb. Setcombine (Setoperation.difference,Newrediskey[2] {"set-2","set-1"}) {Console.WriteLine (item); }        
When you debug the codes,the results is as follow.

The next post of this series are the basic opreation of Sorted Set in Redis.

Basic Tutorials of Redis (4)-set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.