Redis cache Set usage, redis cache set

Source: Internet
Author: User

Redis cache Set usage, redis cache set

In Redis, we can view the Set type as a character Set combination without sorting, which is the same as the List type, you can also add, delete, or determine whether an element exists on the data value of this type. Note that the time complexity of these operations is O (1), that is, the operation is completed within the 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 set, which is exactly the same as the Set container in the C ++ standard library. In other words, if the same element is added multiple times, only one copy of the element is retained in Set. Compared with the List type, the Set type also has a very important feature in terms of functionality, that is, completing aggregation computing operations between multiple Sets on the server side, such as unions, intersections, and differences. Because these operations are completed on the server, the efficiency is extremely high, and it also saves a lot of network I/O overhead. (Reference: http://www.cnblogs.com/stephen-liu74/archive/2012/02/15/2352512.html)

Redis may cache a large number of sets. (You are welcome to make a decision)

Open the redis Server:

Open the redis client:

This is a set!

For the redisset command friends can refer to (http://redisdoc.com)

The following describes how to use redis in. net.

, 1, get the set

1 // obtain all the keys in the setId in the sortset table, and obtain 2 public List <string> GetAllItemsFromSortedSetDesc (string setId) in reverse order) 3 {4 List <string> result = ExecuteCommand <List <string> (client => 5 {6 return client. getAllItemsFromSortedSetDesc (setId); 7}); 8 return result; 9} 10 11 12 public List <string> GetAllItemsFromSortedSet (string setId) 13 {14 List <string> result = ExecuteCommand <List <string> (client => 15 {16 return client. getAllItemsFromSortedSet (setId); 17}); 18 return result; 19} 20 21 22 // obtain all the keys, values23 public IDictionary <string, double> GetAllWithScoresFromSortedSet (string setId) 24 {25 IDictionary <string, double> result = ExecuteCommand <IDictionary <string, double> (client => 26 {27 return client. getAllWithScoresFromSortedSet (setId); 28 // return client. getFromHash <Dictionary <string, string> (hashID); 29}); 30 31 return result; 32}

 

2. delete a set

// Delete the value of a KEY. TRUE public bool RemoveKey (string key) {bool result = false; result = ExecuteCommand <bool> (client => {return client. remove (key) ;}); return result ;}// delete a public bool RemoveItemFromSet (string setId, string item) value in the Set Data) {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 for 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 the 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;        }

 

Here we only share a few methods. In fact, there are many operations on the set.

Using the Sets data structure provided by Redis, you can store some aggregated data. For example, in a Weibo application, you can store all the followers of a user in one collection, store all its fans in a set. Redis also provides operations such as intersection, union, and difference set for the set, which can be easily implemented, such as common concerns, common preferences, and friends of two degrees, you can also use different commands to choose whether to return the result to the client or save the result to a new 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.