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