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

What is the design of distributed cache under .net platform?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how the distributed cache design under the .net platform is like, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Cache is really a good thing, in large systems can effectively improve the speed of the system, this is nonsense, in the .net platform I roughly divided the cache from the function of two categories, data object cache and page output cache. For data caching, it is implemented by the System.Web.Caching.Cache class, and a reference to this object can be obtained from the context object Context.Cache. On the other hand, the page / control output cache is controlled by the .net environment at run time according to the cache declaration of the header. This paper mainly demonstrates some applications and problems related to data caching.

Someone mentioned "the problem that data cannot be shared across Web gardens". Although it is mentioned that the solution is to use XML files to store cached keys, there is a doubt, that is, since .net 's Web garden is process independent, how can it be shared? if so, even objects written to cache keys cached through XML documents cannot be shared in both processes at the same time. The advantage gained here is only to avoid reading the "dirty" cache data that has been invalidated in the current process in other processes, so that opening several Web gardens will produce several cached objects and the utilization of system resources is relatively low. It is even more wasteful if it is deployed with a Web farm, perhaps because few forums have reached this scale and are not within the scope of the design capability. CommunityServer also uses this system object and wraps it to form a CommunityServer.Components.CSCache class, which is good and can be selected for use in the project.

The annotation implementation based on this class also has the NullBackingStore mode in EnterpriseLibrary's CacheBlock, but in order to meet the requirement of multi-process / server co-program caching data, EntLib also provides a solution to use SQL SERVER as a back-end storage device, so that this method can be used when the performance requirements are not too strict and the client connections are not too many. You only need to configure EntLib to work in a shared database partition. All CacheManager instances have read and write rights to cache blocks. Of course, you can also configure to allow only one instance to write and the others to read.

Well, is there a better way? in fact, there is. However, I am surprised that there is no "original" distributed cache solution under the .net platform. Maybe I am ill-informed. Please share what anyone knows. Fortunately, we have Memcached, which has achieved great success on the PHP platform and is an excellent distributed cache solution. You can refer to this article, which should be essential on large sites. Students with examples can take a look, and think of another idea, that is, to expand the IBackingStore interface on the basis of EntLib to derive an implementation from BaseBackingStore, and then through distributed middleware technologies such as Remoting or ICE, similar functions should also be achieved.

It is a good idea to use XML as the storage method of cache keys, so that you can get the corresponding cache keys without scanning when removing cache items in batches. It should be a good solution to integrate with distributed cache.

All right, let's go back and see what Discuzied NT has to do with page caching.

Generally speaking, I don't really like the page output caching feature provided by Net2.0. The main reason is that the expiration of the page cache cannot be controlled manually, and it seems a little uncomfortable to have cache dependencies. In fact, the use of data-bound controls is relatively resource-consuming, the same data I use StringBuilder directly to spell out the output speed is much faster, the test code is relatively simple I will not give it here, you can test it yourself, Discuzable NT also uses a large number of such methods in the design (no wonder the speed is so fast;). Generally speaking, after the template is saved, the corresponding page file will be generated in the aspx directory. For example, you have a page that needs to display the name of a visitor, and its pseudo code may look like this.

Template file content show.html:

The following is the referenced content:

< html > < body > Hello, Your name is <% yourname > < / body > < / html >

Generated file show.aspx

TemplateBuilder.AppendLine ("< html >"); templateBuilder.AppendLine ("< body >"); templateBuilder.AppendLine ("Hello, Your name is" + this.yourname); templateBuilder.AppendLine ("< / body >"); templateBuilder.AppendLine ("< / html >")

Generated file show.aspx

The following is the referenced content:

TemplateBuilder.AppendLine ("); templateBuilder.AppendLine ("); templateBuilder.AppendLine ("Hello, Your name is" + this.yourname); templateBuilder.AppendLine ("); templateBuilder.AppendLine (")

The this.yourname here corresponds to an attribute in the corresponding page background class, which is initialized and assigned by the program at run time, so that the page execution result obtained by * can be obtained from the ToString () method of the templateBuilder object. TemplateBuilder is an instance of the StringBuilder class in the page background class. * * in the OnLoad event after the page execution is completed, according to the page type, such as the home page and the channel home page. The content page, etc., inserts the HTML code of the page execution result into the cache using different cache strategies. When the next request comes in, determine whether the cache is valid in the HttpModule before entering the page life cycle (which also contains the address rewriting function code), and send it back to the client directly from the memory read cache. Of course, the speed is fast, and the execution time seen on the page is naturally 0ms. However, for login users to display different login information so can not use the anonymous version of the cache file, so once you log in the page will really be executed once, but the data to be displayed above have independent cache items, so just reassemble the page code, the speed is still relatively fast, in the official forum to see the home page loading time is 15ms, fast enough.

I think even this time can be saved again. For example, this part of the user login information can generate a JS. When sending the cached version of the anonymous user to the browser, it is judged that if the user logs in, add such a piece of JS code and replace the corresponding HTML in it. You can also use AJAX technology to get it on the client side, which solves the problem of sharing the cache version between logged-in users and non-logged-in users. At least at the home page this level is OK, other main pages should be about the same, I do not know the process of the forum program.

On the other hand, logged-in users should not be slower than anonymous users.

The above content is what the distributed cache design is like on the .net platform. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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

Development

Wechat

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

12
Report