How to store collection types in Redis by Stackexchange.redis list

Source: Internet
Author: User
Tags net domain

Stackexchange is produced by StackOverflow and is a. NET package for Redis, and is used by more and more. NET developers in the project.
The vast majority of developers who used Servicestack have gradually turned around, because SS is no longer open source in its new version, and has limited access to the free version.

Practical issues

Then use. NET developers will find that they do not encapsulate the list type of storage at all, then implement a requirement similar to the following:
If I had a model for the customer.

 Public classCustomer { Public stringFirstName {Get;Set; }  Public stringLastName {Get;Set; }  Public stringAddress1 {Get;Set; }  Public stringCity {Get;Set; }  Public stringState {Get;Set; } }

var New List<customer> ();

How to deposit list<customer> customers into Redis?

Context

Because Stackexchange.redis is a purely client-side agent, he only implements Redis's free functionality and does not encapsulate additional functionality. There is no automatic type matching like ORM.

He only stores key-value pairs like string or byte[]. So you see, you have to serialize the store, just like in JSON format. Protobuf-net, like using third-party Newtonsoft or Google's popular protocol buffers serialization format, is also a good choice.

There are five types of redis supported storage, such as String, Hash, List, set, and Sorted set, which are all composed of strings, as stated above.

Where the set type is not sequential and the values must be unique, the list type is sequential and allows duplicates.

Solution Solutions

If you just want to cache a batch of list<customer> data, encapsulate a listget () and Listset () method yourself.

I have compared the use of List and String two types of storage.

The list type of Redis and. NET domain is also different, in fact, it is a two-way queue that can insert values around.

So if the bulk data is inserted then must be inserted, the code is relatively simple as follows:

//Listset of the package Public voidListset<t> (stringKey, list<t>value) {        .....       //The following database is an object for Redis databases.        foreach(varSingleinchvalue) {                vars = Convertjson (single);//Serialization ofDatabase. Listrightpush (key, s);//to insert a        }}
//Listget of the package Public voidListget<t> (stringkey) { ... //ListRange Returns a set of String objects//need to be deserialized into entities individually varVList =database. ListRange (key); List<T> result =NewList<t>(); foreach(varIteminchvList) { varModel = convertobj<t> (item);//deserializationresult. ADD (model); } returnresult;}

Of course, test the performance, take 20W data average time

The test results are as follows:

Get 10,000 data with an average time of about 793.78 milliseconds.

Of course, you have to try the type of string storage, the code is as follows:

   /// <summary>   ///Storage List/// </summary>   /// <typeparam name= "T" ></typeparam>   /// <param name= "key" ></param>   /// <param name= "value" ></param>    Public voidListset<t> (stringKey, list<t>value) {db.   Stringset (Key, Convertjson (value)); }   /// <summary>   ///gets the list of the specified key/// </summary>   /// <param name= "key" ></param>   /// <returns></returns>    PublicList<t> listget<t> (stringkey) {        returnConvetlist<t>(db.   Stringget (key)); }

Wow! The result was stunned and there was no.

Use a string way to be at least 20 times times faster than the list method.

It seems that different ways of performance difference is quite big.

String is a one-time serialized deserialization, which has fewer inserts and gets deserialized than the list. So it's a good way to access it as a whole.

As for the other types of ways, you can also try.

How to store collection types in Redis by Stackexchange.redis list

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.