On the problem of CacheName in razor

Source: Internet
Author: User

Why use CacheName

Using CacheName mainly takes into account that Razor.parse () will dynamically create an assembly per parse, and if the amount of parsing is large, it will produce many assemblies, and a large number of assembly calls will cause the program to be very slow.

Give me a chestnut:

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

Static voidMain (string[] args) {            stringcshtml = File.readalltext (@"e:\ Baidu Cloud Synchronization Disk \study\net_asp.net\web basic principle \razorcachenametest\htmlpage1.cshtml");  for(inti =0; I < +; i++)            {                stringHTML =Razor.parse (cshtml); } assembly[] ASMs=AppDomain.CurrentDomain.GetAssemblies (); foreach(Assembly ASMinchasms) {Console.WriteLine (ASM. FullName+"\ r \ n");        } console.readkey (); }
How to solve this problem

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

Specify a cachename called CC, and the next parse () parsing will not be recompiled (unless the cshtml content is modified, then the CacheName name will be renamed to Parse () to parse the new file)

 for  (int  i = 0 ; I < 1000 ; I++ //  If you call 100 times, many assemblies are created using the following method, performance is low  string  HTML = Razor.parse (cshtml);                 //  cshtml file parsed I gave a "cache name" is CC, this time once the compilation is successful  //  next time you have to parse () cc, you don't have to repeat the compilation, it will be very fast, Unless the cshtml content modifies the  razor.parse (cshtml, null , "  cc   "            
How to determine cachename to indicate that the file has been modified?

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

 for (int0; i++)            {                string cshtml = file.readalltext (fullPath);                 string cachename = FullPath + file.getlastwritetime (fullPath); // file full path + file Last modified time                string html = Razor.parse (cshtml,null, cachename);                Console.WriteLine (HTML);                Console.readkey ();            }

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

On the problem of CacheName in razor

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.