asp.net template engine Razor CacheName problem Analysis _ practical skills

Source: Internet
Author: User

The example in this article describes the CacheName problem in the ASP.net template engine razor. Share to everyone for your reference. Specifically as follows:

One, why use CacheName

The use of CacheName is mainly to take into account that razor.parse () every time the parse will dynamically create an assembly, if the resolution is large, will produce many assemblies, a large number of assembly calls will cause very slow program.

As an example:

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

static void Main (string[] args)
{
 string cshtml = File.readalltext (@ "e:\ Baidu Cloud Sync disk \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 ();
}

Second, how to solve this problem

When using Razor.parse (), take the CacheName parameter.

Specifies a cachename called CC that will not be recompiled the next time parse () (unless cshtml content is modified, the CacheName name is renamed, so that parse () resolves the new file)

for (int i = 0; i < 1000 i++)
{
  //If invoked 1000 times, many assemblies are created using the following method, low performance
  string html = Razor.parse (cshtml); 
  Parsed cshtml file I give a "cache name" is CC, once the compile success
  //Next time you Parse () cc does not have to compile, the speed will be very fast,
  ///unless cshtml content modification
  razor.parse ( cshtml, NULL, "CC");


Third, how to determine the CacheName said the document has been modified?

There are two ways, one is the file full path + file modification time, can also be based on the cshtml file MD5 value.

for (int i = 0; i < i++)
{
  string cshtml = File.readalltext (fullpath);
  String cachename = FullPath + file.getlastwritetime (fullpath);
  File full path + file Last modified at time
  string html = Razor.parse (cshtml,null,cachename);
  Console.WriteLine (HTML);
  Console.readkey ();
}

Whenever the cshtml file is modified, the value of the cachename is changed, and Parse () determines whether to recompile based on the CacheName value. If the cshtml file was modified three times during the test, three assemblies would eventually be generated, and if the cshtml file was not modified, there would be only one assembly.

Note: questions about CacheName.

After the experiment found, even if the cachename written a fixed value, when the cshtml changed, the result of parse is also modified content, this is why?

After the decompile we found that the parse method finally called the Templateservice GetTemplate method, the code is as follows:

 private itemplate gettemplate<t> (string razortemplate, object model, string CAC
 Hename) {func<string, cachedtemplateitem, cachedtemplateitem> 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 (model = = null) razortemplate 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 is to find out whether there is a cached item "TryGetValue (CacheName, out of item)" That has a name equal to CacheName from the cached cache, and if it does not exist, compile and create; Then the hashcode of the cshtml content in the cache (the character of the string, the hashcode of the same string, the same probability of the hashcode of the different strings) is the same as the razortemplate of the hashcode that was passed in. If it is not the same, recompile the creation without using the cache.

So this can explain why with a fixed cachename, as long as you modify the content of cshtml, still will parse new content.

Some students asked: since the revision of cshtml, it will be parse new content, that to cachename what is the significance of it? This is because the hashcode of the same probability of different strings is very low, but not without "A, b two strings are not the same, but hashcode the same" this possibility, so if only rely on hashcode, then there is such a probability "cshtml file modified, But the modified hashcode is the same as before, so parse still executes the old logic. So plus cachename is "double insurance."

I hope this article will help you with the ASP.net program design.

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.