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 solve the pit of C # Memory Cache

2025-04-09 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 solve the pit of C # Memory Cache". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to solve the hole of C# Memory Cache"!

Background

A few days ago, the number of visits to the company's server database was on the high side, and the operation and maintenance staff received an alarm and arranged for our team partners to investigate the reasons.

We find that the original system runs a regression test periodically, which runs more tasks, and each task is processed to get relevant data from the database. High-speed regression testing also brings high-frequency database reading.

Solution 1

We think that the data to be fetched by each task is very different, so we consider modifying this process and adding MemoryCache to cache the data read in the database.

The whole modification is very simple, and I believe it is a piece of cake for all the bosses who hang out in the blog park all the year round, so I will no longer describe the details of the steps to add the cache.

From the addition of cache, code submission, Teamcity compilation, to the test environment, the installation of QA environment is very smooth.

Well, excellence is a habit and there is nothing we can do about it.

Life is like a play, when we are still immersed in the confidence that "the Cache I added can't be BUG again", QA brought bad news and a large number of regression tests failed.

Troubleshooting

We were accustomed to using Redis caching before, so common sense tells us that if the data in the database is unchanged, the effect of the data read after adding the cache is exactly the same as that of reading from the database.

Unless, unless the common sense is wrong.

So we added a log to compare the data read before and after writing to the cache, and the results were unexpected.

Damn it, MemoryCache ruined my face, lost my precision, and cost my life!

From the log, we can see that the first row is the result read from the database, the second row is read from cache, the first two pieces of data are exactly the same, to the third, fourth, fifth, careful observation found that after the decimal point, there are some relatively small changes after the decimal point, no matter the size of the change, but the data does change, so MemoryCache will affect the accuracy of the data? What is the use of MemoryCache, which will change the accuracy of the data?

Witty, I seem to have seen through everything, this is definitely not the pot of MenoryCache!

Different MemoryCache

I picked out the source code of MemoryCache from https://referencesource.microsoft.com to find out.

When we locate the AddOrGetExisting method in MemoryCache, we can see that the essence of the process of storing data in the cache is to store the object in a Hashtable called _ entries. Similarly, the data is also fetched into the Hashtable through Key, and the whole process does not serialize and reverse sequence the object, nor clone the object. This means that what we deposit before, and what we take out later (no matter how many times we fetch data from MemoryCache), always take out the same object.

This is very different from the RedisCache I used before. When we store data in Redis, we serialize the object and store it in Redis. When we get the data, the byte data in Redis is inversely sequenced into an object, which means that the previous deposit and the last take-out are no longer the same object, so the data in Redis is safe.

Conjecture

I made a bold guess that the reason why the data extracted from the MemoryCache had changed may be what modification was made to the object during the complex processing after the object was removed, so in the later stage, when the data was read from the database again, the data read was no longer the data originally stored, but the data after the previous modification. With this conjecture, I modified the code.

Solution 2

Clone () the result after fetching the data from MenoryCache, so that even if the program modifies the result, it will not affect the data in Cache.

Once again, after the submission, compilation, and installation, the regression test passed smoothly.

At this point, I believe you have a deeper understanding of "how to solve the pit of C # Memory Cache". 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