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 problem of cacheName in asp.net template engine Razor

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

Share

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

This article mainly explains "how to solve the problem of cacheName in asp.net template engine Razor". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of cacheName in asp.net template engine Razor".

The details are as follows:

First, why use cacheName

The main reason for using cacheName is that Razor.Parse () dynamically creates an assembly each time it parses. If the amount of parsing is large, it will produce a lot of assemblies, and a large number of assembly calls will cause the program to be very slow.

For example:

If you compile 1000 times, the compilation speed will be very slow.

Static void Main (string [] args) {string cshtml = File.ReadAllText (@ "E:\ Baidu Cloud Synchronizer\ Study\ Net_ASP.NET\ Web fundamentals\ RazorCacheNameTest\ HTMLPage1.cshtml"); for (int I = 0; I

< 1000; i++) { string html = Razor.Parse(cshtml); } Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { Console.WriteLine(asm.FullName+"\r\n"); } Console.ReadKey();} 二、如何解决这个问题 使用Razor.Parse()时,带上cacheName参数。 指定一个cacheName叫cc,下次Parse()解析时就不会重新编译了(除非cshtml内容修改,那么cacheName名也要重新命名,让Parse()解析新文件) for (int i = 0; i < 1000; i++){ //如果调用1000次,使用下面方式就会创建很多程序集,性能很低 string html = Razor.Parse(cshtml); //解析的cshtml文件我给的一个"缓存名"是cc,这次一旦编译成功 //下次再让你Parse() cc就不用重复编译了,速度会非常快, //除非cshtml内容修改 Razor.Parse(cshtml, null, "cc");} 三、怎么确定cacheName表示的文件已修改呢? 有两种方式,一种就是文件全路径+文件修改时间,还可以根据cshtml文件的MD5值。 for (int i = 0; i < 10; i++){ string cshtml = File.ReadAllText(fullPath); string cacheName = fullPath + File.GetLastWriteTime(fullPath); //文件全路径+文件上一次被修改时间 string html = Razor.Parse(cshtml,null,cacheName); Console.WriteLine(html); Console.ReadKey();} 每当cshtml文件被修改,cacheName的值就会改变,Parse()根据cacheName值判断是否重新编译。假如测试过程中对cshtml文件做了三次修改,最终会生成三个程序集,如果cshtml文件未修改,最后只有一个程序集。 注意:关于cacheName的问题。 经过试验发现,即使cacheName写成一个固定的值,当cshtml发生改变的时候Parse的结果也是修改后的内容,这是为什么呢? 经过反编译我们发现Parse方法最终调用的是TemplateService的GetTemplate方法,代码如下: private ITemplate GetTemplate(string razorTemplate, object model, string cacheName){ Func updateValueFactory = null; CachedTemplateItem item; if (razorTemplate == null) { throw new ArgumentNullException("razorTemplate"); } int hashCode = razorTemplate.GetHashCode(); if (!this._cache.TryGetValue(cacheName, out item) || (item.CachedHashCode != hashCode)) { Type templateType = this.CreateTemplateType(razorTemplate, (model == null) ? typeof(T) : model.GetType()); item = new CachedTemplateItem(hashCode, templateType); if (updateValueFactory == null) { updateValueFactory = (n, i) =>

Item;} this._cache.AddOrUpdate (cacheName, item, updateValueFactory);} return this.CreateTemplate (null, item.TemplateType, model);}

The main idea of the code is to find out whether there is a cache item "TryGetValue (cacheName, out item)" whose name is equal to cacheName in the cache cache, and if it does not exist, compile and create If it exists, then check whether the hashCode of the cshtml content in the cache (the signature of the string is the same as the HashCode of the same string, and the HashCode of different strings has the same probability) and whether the HashCode of the razorTemplate passed in this time is the same. If it is not the same, it is also recompiled and created without using the cache.

So this explains why using a fixed cacheName, as long as you modify the content of the cshtml, it will still Parse the new content.

Some students will ask: since the modification of cshtml, will re-Parse new content, then what is the point of cacheName? This is because the probability that the HashCode of different strings is the same is very low, but it is not without the possibility that the two strings An and B are different, but the hashcode is the same, so if you only rely on HashCode, then there is such a probability that "the file of cshtml has been modified, but the modified HashCode is exactly the same as before, so Parse still executes the old logic." So adding cacheName is "double insurance".

Thank you for your reading, the above is the content of "how to solve the problem of cacheName in asp.net template engine Razor". After the study of this article, I believe you have a deeper understanding of how to solve the problem of cacheName in asp.net template engine Razor. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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