In Redis, how does one store a List of Collection types through StackExchange. Redis?

Source: Internet
Author: User

In Redis, how does one store a List of Collection types through StackExchange. Redis?

StackExchange, produced by StackOverFlow, is a. NET encapsulation of Redis and is used by more and more. NET developers in projects.
Most developers who used ServiceStack gradually switched over Because SS is no longer open-source in its new version and has restrictions on the free version.

Actual Problems

Developers using. NET will find that there is no storage encapsulation for the List type at all, so they need to implement a similar requirement:
Assume that I have a Customer model.

    public class Customer    {        public string FirstName { get; set; }        public string LastName { get; set; }        public string Address1 { get; set; }        public string City { get; set; }        public string State { get; set; }    }

 

var customers = new List<Customer>();

How to store List <customer> MERs into Redis?

Ins and outs

Because StackExchange. Redis is a pure client proxy, it only implements the Redis free function and does not encapsulate other functions. It does not have automatic type matching like ORM.

It only stores key-value pairs like string or byte []. So you understand, the storage must be serialized, in Json format. Protobuf-Net, like using a third-party NewtonSoft or Google's popular Protocol Buffers serialization format, is also a good choice.

Redis supports five types of storage: String, Hash, List, Set, and Sorted Set. As mentioned above, all these storage types are composed of strings.

The Set type has no sequence, and the values must be unique. The List type has sequence and can be repeated.

Solution

If you only store a batch of List <Customer> data to cache, encapsulate a ListGet () and ListSet () methods.

I have compared two types of storage: List and String.

The List type of Redis is different from that in the. NET field. In fact, it is a bidirectional queue that can insert values left and right.

Therefore, for batch data insertion, data must be inserted one by one. The code is relatively simple as follows:

// The encapsulated ListSetpublic void ListSet <T> (string key, List <T> value ){..... // The following database is the redis database object. foreach (var single in value) {var s = ConvertJson (single); // serialize the database. listRightPush (key, s); // insert one by one }}
// The encapsulated ListGetpublic void ListGet <T> (string key ){... // ListRange returns a set of string objects // The object var vList = database needs to be deserialized one by one. listRange (key); List <T> result = new List <T> (); foreach (var item in vList) {var model = ConvertObj <T> (item ); // deserialization result. add (model) ;}return result ;}

Of course, I tested the performance and obtained an average time of 20 million pieces of data.

The test results are as follows:

Obtain 10000 data records, with an average time of about 793.78 milliseconds.

 

Of course, you must also try the String type storage. The Code is as follows:

/// <Summary> /// storage List /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "key"> </param> // <param name = "value"> </param> public void ListSet <T> (string key, list <T> value) {db. stringSet (key, ConvertJson (value ));} /// <summary> /// obtain the List of specified keys /// </summary> /// <param name = "key"> </param> // <returns> </returns> public List <T> ListGet <T> (string key) {return ConvetList <T> (db. stringGet (key ));}

Wow! The result was stunned.

The String method is at least 20 times faster than the List method.

It seems that the performance of different methods is quite different.

String is a one-time deserialization, which is less than the List one-by-one insertion and acquisition of deserialization. Therefore, it is still a good method for overall access.

For other types of methods, you can try it.

 

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.