One of mvc3 cache: Use Page Cache

Source: Internet
Author: User

In previous webform development, you can add outputcache to the page header to enable page caching. In mvc3, if you use the razor template engine, how can you use page caching?

How to enable

To enable page caching in mvc3, add an outputcache Attribute before the corresponding action on the page.

Let's create a demo to test it. In this demo, enter the current time on the page in index. cshtml under the Home Directory of view.

@{
Layout = NULL;
}
<! Doctype html >
< Html >
< Head >
< Title > Index </ Title >
</ Head >
< Body >
< Div >
< H2 >
Current Time: @ datetime. Now. tostring ("T ") </ H2 >
</ Div >
</ Body >
</ Html >

Add the corresponding action in controllers and add the outputcache attribute.

[Handleerror]

Public   Class Homecontroller: Controller
{
[Outputcache (Duration =   5 , Varybyparam =   " None " )]
Public Actionresult index ()
{
Return View ();
}
}

Refresh the page to see that the page has been cached for 10 seconds. When the data on the page does not need to be presented to the user in real time, such page cache can reduce the real-time data processing and requests. Of course, this is the cache for the entire page, the cache granularity is still coarse.

Cache location

You can set the cache location attribute to determine where the cache is stored.

You can set the following attributes for location:

· Any · client · downstream · server · none · serverandclient

The default value of location is any. We recommend that you store user-side information on the client side, and some public information on the server side.

The location should be like this.

[Handleerror]
Public   Class Homecontroller: Controller
{
[Outputcache (Duration =   5 , Varybyparam =   " None " , Location = Outputcachelocation. Client, nostore =   True )]
Public Actionresult index ()
{
Return View ();
}

}Cache dependency

Varybyparam can set cache dependencies for the cache. For example, a product details page may be a cache page based on the product ID.

The cache dependency should be set as follows.

In mvc3The output cache is improved. You do not need to manually specify varybyparam for outputcache. The parameter of action is automatically used as the cache expiration condition. (Thanks for the reminder)

[Handleerror]
Public   Class Homecontroller: Controller
{
[Outputcache (Duration =   Int . Maxvalue, varybyparam =   " ID " )]
Public Actionresult index ()
{
Return View ();
} }
Another common setting method

When you need to set multiple actions in a unified manner, you can configure them in the web. config file and then apply them.

Configure the caching node in Web. config

< Caching >
< Outputcachesettings >
< Outputcacheprofiles >
< Add Name = "Cache1hour" Duration = "3600" Varybyparam = "NONE" />
</ Outputcacheprofiles >
</ Outputcachesettings >

</Caching>

You can use this configuration node on action. This method is convenient for unified configuration management.

[Handleerror]
Public   Class Homecontroller: Controller
{
[Outputcache (cacheprofile =   " Cache1hour " )]
Public Actionresult index ()
{
Return View ();
} }

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.