When Razor uses Parse (), it is best to specify the "cache name", razorparse

Source: Internet
Author: User

When Razor uses Parse (), it is best to specify the "cache name", razorparse
Why?

The title of this article clearly has a reminder.

From 18 years of life experience, if you want to remind people what to do, don't do it.

It is best to explain the cause. Then let's explain why.

Well, before explaining the cause, Let's explain the background and use the Razor template engine in a non-mvc scenario. (End)

This is still the flowchart. We know that the Razor template engine generates C # code execution, generates an assembly, and then calls the Assembly to generate html.

 

That is to say, if there is no accident, an assembly will be generated for each request. (The code in the yellow circle is executed repeatedly .)

However, we know that the generation of these assemblies is very time-consuming, and multiple Assembly programs occupy resources.

This is why the editor emphasizes the need to specify the cache name. Then let's take a look at how to use the "cache ".

In fact, it is complicated and easy to do!

 

How to Use

1. The specified cache name will become faster

Such a piece of code.

String cshtml = File. ReadAllText ("path ");

For (int I = 0; I <500; I ++)

{

// In this example, 500 randomly named assemblies are formed.

String html1 = Razor. Parse (cshtml );

// In this example, parse the cshtml file and give a "cache name ".

// Once the compilation is successful this time, the next Parse () will not repeat the compilation. Until the cshtml file is modified.

String html2 = Razor. Parse (cshtml, null, "cccc"); // "cccc" is a random name.

}

The "cache name" just mentioned may be a bit vague and I don't know how to call it. In the preceding example

"Cccc", after this parameter is given, the same request will no longer be created with a random name.

2. What is the proper name?

In this example, we use a policy of [full path of the cshtml file] + [last modification time of the file.

Of course, you can also use the file's md5 or something.

I understand that only one cshtml file can be identified. (Of course, more than the same name. The file is not the same before and after modification .)

String fullpath = "full path ";

String cshtml = File. ReadAllText (fullpath );

// "Cache name" = [full cshtml file path] + [last file modification time]

String cacheName = fullpath + File. GetLastWriteTime (fullpath );

Razor. Parse (cshtml, null, cacheName );

3.Encapsulate a RazorHelper

Based on the above ideas 1 and 2 (1. Specify "cache name" 2. "cache name" = [full path of cshtml files] + [last modification time of files ])

The problem discussed in this article has been solved, but after operations 1 and 2, a lot of code will be added, and the code can be reused.

Therefore, encapsulate a RazorHelper to throw the corresponding code into a method.

Public class RazorHelper

{

// You can directly comment out the document without.

Public static string Parse (HttpContext context,

String cshtmlVirtualPath, Object model)

{

String fullpath = context. Server. MapPath (cshtmlVirtualPath );

String cshtml = File. ReadAllText (fullpath );

String cacheName = fullpath + File. GetLastWriteTime (fullpath );

String html = Razor. Parse (cshtml, model, cacheName );

Return html;

}

}

Of course, if RazorHelper only has such a method, it's a bit funny...

In the next few articles, I will gradually enrich them.

 

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.