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

The necessity and implementation method of redis data caching consistency

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Today, what the editor shares with you is the necessity and implementation of consistent redis data caching, which many people do not quite understand. Today, in order to let you know more about redis data caching, the editor summarizes the following, and let's move on. I'm sure you'll get something.

The so-called redis data consistency means that when the data in redis is modified, saved or deleted, the data in redis should be changed accordingly, otherwise users are likely to query the deleted dirty data when they query again.

First, the necessity of cache consistency

Still speaking from the previous article, we have solved the problem of redis cache penetration (a simple solution that can be optimized again), but we also need to focus on the issue of cache consistency when using redis. For example, after saving a new user, we should also insert this piece of data in the redis cache, and update a piece of data in the cache. The default practice of redis is: when you do not set up, the value stored in redis is the data you stored before, and the data will be synchronized only when you restart the server. Obviously, this is very undesirable. If so, you have to restart the server all the time. What a disaster it will be!

Second, business scenarios

Let me just talk about one scenario. The other scenarios are the same. The scenario is as follows: suppose we put user data into redis, when a new user is registered, a new data will be inserted into the database. At the same time, the data should also be inserted in redis to display the latest data when the next query is made.

Let's first take a look at whether redis will do consistency operations for us without any processing by default. Now that I have 18 pieces of user data in my test database, we start the system and register to insert the 19th item of data to see if there are 19 items in the redis.

After registering, I now insert the 19th piece of data into the database.

Then we use the query caching method mentioned in the previous article to get all the information. If we look directly at the last item, we can see that it is Runqing rather than 19 in the end, so redis does not do consistent caching operations for us by default.

III. Implementation of cache consistency (scheme 1)

In fact, it is very simple to achieve consistency. After the insert operation, we take out the data and save it to the redis cache, so that we can see the new data when we query the cache again. The code is as follows:

It should be noted that I am calling findAll () of JPA directly. In fact, it is better to update the newly inserted one according to id, which is more efficient. Here is just a demonstration of how to implement it. Then register a new piece of data to see if you can see it in the cache at the same time: the newly inserted data id is 29. Let's take a look at whether the new data exists in the cache.

4. Implementation of redis cache consistency (scheme 2)

Regularly clear the data in redis, for example, set a scheduled task, every hour will clear the data in redis, that is, make the data in redis invalid, and then save or delete the data in redis before it no longer exists, so it is equivalent to reset the data to redis, so you can ensure the consistency of the data.

The above is a brief introduction to the necessity and implementation of consistent redis data cache. of course, the differences in the detailed use of the above have to be understood by everyone. If you want to know more, welcome to follow 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

Database

Wechat

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

12
Report