Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to store collection type List through StackExchange.Redis in Redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to store the collection type List in Redis through StackExchange.Redis. The content is concise and easy to understand. It will definitely make you shine. I hope you can gain something from the detailed introduction of this article.

StackExchange is produced by StackOverFlow and is a. NET wrapper over Redis, used by an increasing number of. NET developers in their projects.

Most developers who originally used ServiceStack have gradually switched over, as SS is no longer open source in its new version and has restrictions on free versions.

practical problems

Then developers using. NET will find that they have no storage encapsulation for List types at all, so to implement a requirement similar to the following:

Suppose 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();

How to store List customers in Redis?

ins and outs

Because StackExchange.Redis is a pure client proxy, it only implements Redis free features, and does not encapsulate other features. There is also no automatic type matching like ORM.

It only stores key-value pairs like string or byte[]. So you see, it has to be serialized, stored in a format like Json. Protobuf-Net, which uses 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, these storage types are all composed of strings.

The Set type has no order and the value must be unique, and the List type has order and allows duplication.

solutions

If you only want to store a batch of List data for caching, wrap a ListGet() and ListSet() method yourself.

I compared using List and String storage types.

The List type of Redis differs from the. NET domain in that it is, in fact, a bidirectional queue that inserts values left and right.

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

//Encapsulated ListSet

public void ListSet(string key, List value)

{

.....

//The database below is the database object of redis.

foreach (var single in value)

{

var s = ConvertJson(single); //serialize

database.ListRightPush(key, s); //insert them one by one

}

}

//Encapsulated ListGet

public void ListGet(string key)

{

...

//ListRange returns a string object

//needs to be deserialized into entities one by one

var vList = database.ListRange(key) ;

List result = new List();

foreach (var item in vList)

{

var model = ConvertObj(item); //deserialize

result.Add(model);

}

return result;

}

Of course, the performance was tested, and the average time of 20W data was taken.

The test results are as follows:

10000 pieces of data were acquired in an average time of 793.78 milliseconds.

///

///Storage List

///

///

///

///

public void ListSet(string key, List value)

{

db.StringSet(key, ConvertJson(value));

}

///

///Get List of specified key

///

///

///

public List ListGet(string key)

{

return ConvetList(db.StringGet(key));

}

The above is how to store the collection type List in Redis through StackExchange.Redis. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report