How Should Redis be used? (3 )?, Redis Exploitation

Source: Internet
Author: User

How Should Redis be used? (3 )?, Redis Exploitation

I have benefited a lot from the last two articles. I decided to add this article in the spirit of hitting the ground and not digging the deepest point. In the last article, I mentioned that implementing Hierarchical Cache Management should be a feasible solution. Therefore, I have put it into practice today. However, some problems are also found after the cache classification, such:

After appServerA modifies the data and synchronizes it to Redis/DB, how can appServerB update the local cache? Although Redis was designed to solve this problem, MemoryCache still needs to be retained in the classification scheme. So how to save it? I tried the following methods. Now let's look at them one by one.

Full incremental Synchronization

Full data verification means that all cached data is first synchronized to Redis and then synchronized based on the time stamp of the data. The decomposition steps are as follows:

What should we do if we implement the above scheme to. NET + Redis?

The first step is easy to skip, and the second step is a bit problematic. This timestamp should be easy for redis to sort and obtain. Considering these problems, I think it is more reliable to use the Linux digital timestamp, it looks good to use SortSet to create indexes and synchronize indexes. Let's look at the Code:

This article uses User as an example. It may be unsuitable for scenarios and you will understand it.

Public void UpdateToRedis (User user) {var redis = ConnectionMultiplexer. connect ("localhost"); var db = redis. getDatabase (); TimeSpan ts = DateTime. now. toUniversalTime ()-new DateTime (1970, 1, 1, 0, 0, 0, 0); double time = Convert. toInt64 (ts. totalSeconds); // calculate the Unix timestamp db. sortedSetAdd ("capqueen: user: index", user. id, time); // update record var json = JsonConvert. serializeObject (user); db. stringSet ("capqueen: user" + user. id, json); // user record}

Synchronization:

Public void Sync (double timespan) {var redis = ConnectionMultiplexer. connect ("localhost"); var db = redis. getDatabase (); TimeSpan ts = DateTime. now. toUniversalTime ()-new DateTime (1970, 1, 1, 0, 0, 0, 0); double time = Convert. toInt64 (ts. totalSeconds); // calculate the Unix timestamp // synchronize records within the time range. The time range is added here to prevent inaccurate data. var members = db. sortedSetRangeByScore ("capqueen: user: index", timespan, time); var keys = members. to List (). select <RedisValue, RedisKey> (I => I. toString (); var values = db. stringGet (keys. toArray (); // construct the List Json to accelerate the parsing of var portsJson = new StringBuilder ("["); values. toList (). forEach (item => {if (! String. isNullOrWhiteSpace (item) {portsJson. append (item ). append (",") ;}}); portsJson. append ("]"); var users = JsonConvert. deserializeObject <List <User> (portsJson. toString (); // synchronize with the memory List <User>... // END}

The above code is just an instance, and it is not very reliable during actual operation.

Message notification

Incremental synchronization is good, but the timing is also a problem. incorrect timing will affect the user experience. We still cannot control the data. Is there a way to promptly notify the appServer to update the cache? Here, the. NET Event mechanism is emerging in my mind.

I think. NET has an excellent Event mechanism, which makes it very convenient for me to use. NET.

However, this is the communication between servers. I think Scoket does not belong to it, and Scoket has been used for two-way communication between servers. This scenario naturally reminds me of this solution. However, to add a socket, the complicated architecture is more complicated, and it is more appropriate to find a reliable middleware. If you have read the Redis function list, you must have forgotten that Redis has another subscription release, pub/sub function. So I used this function and designed the following solution:

Let's take a look at the pub/sub function of Redis. Please refer to the documentation.

Redis provides the subscription and publishing function for a specified channel. Each Client can subscribe to a specified channel message or send a message to a specified channel.

The pub/sub function of ServiceStack. Redis is implemented as follows:

Using (var redisConsumer = new RedisClient (TestConfig. singleHost) using (var subscribe = redisConsumer. createsubtasks () {subtasks. onSubscribe = channel =>{// subscribe event}; subscribe. onUnSubscribe = channel =>{// unsubscribe event}; subscribe. onMessage = (channel, msg) =>{// msg here, I defined userid var user = redisConsumer for testing. as <User> (). getById (msg); // obtain the record UpdateLocalCache (user) from Redis; // update local}; subscribe. subscribeToChannels ("capqueen: redis: events"); // blocking}

 

Send:

Using (var redisPublisher = new RedisClient ("localhost") {redisPublisher. PublishMessage ("capqueen: redis: events", userId); // send message}

 

Here I think the message should be defined as a message body, and the receiving and processing should be specific based on the specific message definition, because the subscription and publishing channel should obviously be used properly. If a business is a channel, A little too much.

Of course, this implementation will be further explained. I will not describe it here.

Summary

In this article, we have made some implementations based on the solution mentioned in the previous article. There are too many shortcomings. For example, what if an event is lost? It is important to properly process missing messages and to delete records. I haven't thought of the solution yet. I don't know how the Elders think about it. I may have abused Redis for technical purposes, but I don't try to hit the wall and how can I grow. I feel a little lost in my coffin. Please kindly advise me!

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.