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 use Redis

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

Share

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

This article mainly explains "how to use Redis". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Redis.

Working with scen

In my project, there is a function provided to Autocomplete, and the amount of data is about tens of thousands. In this article, I use an example of name retrieval to illustrate the list. Please stamp the Demo from the Redis author.

The list is full of user names, for example, we have a user object in our system:

Public Class User {public string Id {get; set;} public string Name {get; set;}.... Public string UserHead {get; set;}}

The system needs a drop-down list of users, because the large amount of data can not be displayed at once, so an AutoComplete function is added. If you do not need a centralized cache such as Redis, but directly cache it in local memory, the structure is as follows:

Var users = new List {...}; / / read a list of users MemoryCache.Set ("capqueen:users", users); / / put in memory / / read var users = MemoryCache.Get ("capqueen:users")

Because they are all in memory, you can just save List directly, and you can also search as follows:

Var findUsers = users.Where (user = > user.Name.StartWith ("A")) .ToList (); for example, the character entered is "A"

It's quite simple, and you don't have to think about how to store the data structure. But after switching to centralized caching services such as Redis, let's think about how to store it.

Scheme 1: similar to memory cache implementation.

The Redis link library used in this article is StactkExchange.Redis, an open source product of StackOverFlow.

Var db = redis.GetDataBase (); / / get 0 database var usersJson = JsonConvert.SerializeObject (users) / / serialization db.StringSet ("capqueen:users", usersJson); / / Storage var usersString = db.StringGet ("capqueen:users"); var userList = JsonConvert.DeserializeObject (users); / / deserialization

The above method is logically fine, and the compilation can also be passed. But when you think about it, Redis is separate from appSever as a separate caching service, and this way of reading is a burden on the IO of the redis server, even if it is much slower than the local memory cache.

Then how to solve the problem? Imagine that the essence of key-value is Key, so for List, item should be stored separately.

Scheme 2: Keys fuzzy matching.

After looking at the command documentation of Redis (see reference 4), I found the command Keys, and immediately modified the scheme. First of all, we need to establish the keyword to search as key, here I define key as "capqueen:user: {id}: {name}", where {} is to be replaced with the corresponding attribute of item. The code is as follows:

Var redis = ConnectionMultiplexer.Connect ("localhost"); var db = redis.GetDatabase (); var users = new List {new User {Id = 6, Name = "aaren", Age=10}, new User {Id = 7, Name = "issy", Age=11}, new User {Id = 8, Name = "janina", Age=13}, new User {Id = 9, Name = "karena", Age=14}} Users.ForEach (item = > {var key = string.Format ("capqueen:user: {0}: {1}", item.Id, item.Name); var value = JsonConvert.SerializeObject (item); db.StringSet (key, value);})

All user is stored as a separate Key-Value, so how to use Keys search? Let's take a look at Redis's Keys command:

KEYS pattern finds all key that match the given schema pattern. KEYS * matches all the key in the database. KEYS h?llo matches hello, hallo, hxllo and so on. KEYS h*llo matches hllo, heeeeello, and so on. KEYS h [ae] llo matches hello and hallo but does not match hillo. Special symbols are separated by\

In other words, Keys can perform simple fuzzy matching, so our search here can be changed to the following way:

Var redis = ConnectionMultiplexer.Connect ("192.168.10.178"); var db = redis.GetDatabase (); var server = redis.GetServer ("192.168.10.178", 6379); var keys = server.Keys (pattern: "capqueen:user:*:a*"); var values = db.StringGet (keys.ToArray ()); / / deserialization var jsonValues = new StringBuilder ("["); values.ToList () .ForEach (item = > jsonValues.Append (item) .append (",")) JsonValues.Append ("]"); var userList = JsonConvert.DeserializeObject (jsonValues.ToString ())

Note that in the above code, because each value is a json, in order to increase the efficiency of the conversion, I first deal with the json arry and then deserialize it.

This solution does solve my current problem, but I noticed a passage in the Redis document:

KEYS is very fast, but using it in a large database can still cause performance problems. If you need to find a specific key from a dataset, you'd better use Redis's set instead.

At this point, I believe you have a deeper understanding of "how to use Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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