The Redis cache set uses

Source: Internet
Author: User
Tags redis server

In Redis, we can see the set type as an unordered character set, and as with the list type, we can also perform operations such as adding, deleting, or judging whether an element exists on the data value of that type. It should be stated that the time complexity of these operations is O (1), which is the completion of the operation within a constant time. The maximum number of elements that a set can contain is 4294967295.
Unlike the list type, duplicate elements are not allowed in the set collection, which is identical to the set container in the C + + standard library. In other words, if you add the same element more than once, only one copy of the element will be preserved in set. The set type has a functionally important feature in comparison to the list type, which is to perform aggregate compute operations between multiple sets on the server side, such as unions, intersections, and differences. Because these operations are done on the server side, they are highly efficient and also save a lot of network IO overhead. (Reference: http://www.cnblogs.com/stephen-liu74/archive/2012/02/15/2352512.html)

Redis makes cache set possible to more ( opinion, welcome to shoot Bricks )

Open Redis Server:

To open the Redis client:

This is a set set!

As for Redisset's command, little Friends can refer to (http://redisdoc.com)

Share how Redis is used in. NET below

, 1, Get set

1   //get all keys in SetID in the Sortset table, in reverse2         Publiclist<string> Getallitemsfromsortedsetdesc (stringsetId)3        {4list<string> result = executecommand<list<string>> (client =5             {6                 returnclient. Getallitemsfromsortedsetdesc (setId);7             });8             returnresult;9         }Ten  One  A          Publiclist<string> Getallitemsfromsortedset (stringsetId) -         { -list<string> result = executecommand<list<string>> (client = the             { -                 returnclient. Getallitemsfromsortedset (setId); -             }); -             returnresult; +         } -  +  A         //get all keys,values in SetID in the Sortset table at          Publicidictionary<string,Double> Getallwithscoresfromsortedset (stringsetId) -         { -idictionary<string,Double> result = executecommand<idictionary<string,Double>> (client = -             { -                 returnclient. Getallwithscoresfromsortedset (setId); -                 //return client. Getfromhash<dictionary<string, string>> (Hashid); in             }); -  to             returnresult; +}

2, delete a set

        The value of a key was deleted, and a successful return of true public        bool RemoveKey (string key)        {            bool result = false;            result = executecommand<bool> (client + =                 {                     return client). Remove (key);                 });            return result;        }        Delete a value of item in the Set data public        bool Removeitemfromset (string setId, string item)        {            byte[] bvalue = System.Text.Encoding.UTF8.GetBytes (item);            BOOL result = executecommand<bool> (client = =            {                var rc = client as redisclient;                if (RC! = null)                {                    return RC. Srem (setId, bvalue) = = 1;                }                return false;            });            return result;        }

3, search

         Search key public        list<string> Searchkeys (string pattern)        {            list<string> result = executecommand<list<string>> (client =            {                return client). Searchkeys (pattern);            });            return result;        }

4, add an element to set

       public bool Additemtoset (string setId, string item)        {            byte[] bvalue = System.Text.Encoding.UTF8.GetBytes ( Item);            BOOL result = executecommand<bool> (client = =            {                var rc = client as redisclient;                if (RC! = null)                {                    return RC. Sadd (setId, bvalue) = = 1;                }                return false;            });            return result;        }

There are only a few ways to share this, but there are a lot of ways to do that with set.

Using the sets data structure provided by Redis, you can store some aggregated information, such as in a microblog application, where you can have a collection of all the followers of a user and a collection of all their fans. Redis also provides for the collection of intersection, set, difference sets and other operations, can be very convenient to achieve such as common concern, common preferences, two-degree friends and other functions, to all of the above collection operations, you can also use different commands to choose whether to return the results to the client or save set into a new collection.

The Redis cache set uses

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.