Home >Web Front-end >HTML Tutorial >MVC3 cache (1): page cache_html/css_WEB-ITnose

MVC3 cache (1): page cache_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:53:181285browse

Enable page caching


If you want to enable page caching in MVC3, you need to add Action is preceded by an OutputCache attribute.

@{    ViewBag.Title = "主页";}<!DOCTYPE html><html><head>    <title>页面缓存</title></head><body>	现在时间:@DateTime.Now.ToString("T")</body></html>

Add the corresponding Action in the Controller and add the OutputCache attribute.

[OutputCache(Duration=5, VaryByParam="none")]        public ActionResult Index()        {            return View();        }

Refresh the page and you will see that the page has been cached for 10 seconds. When the data in the page does not need to be presented to the user all the time, such page caching can reduce the time of data processing and requests. Of course, this is a cache for the entire page, and the cache strength is still relatively rough.


The cached location


can be passed Set the cache's Location property to determine where to place the cache.

The properties that can be set by Location are (Any Client Downstream Server None ServerAndClient)

The default value of Location is Any. It is generally recommended to store user information on the Client side and some public information on the Server side.

Added Location it should look like this.

[OutputCache(Duration=5, VaryByParam="none",Location=OutputCacheLocation.Client ,NoStore=true)]        public ActionResult Index()        {            return View();        }

General settings in configuration file


When we need to make unified settings for multiple Actions, we can apply them after unified configuration in the web.config file.

Configure the Caching node in web.config

<caching><outputCacheSettings>    <outputCacheProfiles>        <add name="Cache1Hour" duration="3600" varyByParam="none"/>    </outputCacheProfiles></outputCacheSettings></caching>

Then use the configuration node on Action. This method is better for unified management of configuration information. convenient.

[OutputCache(CacheProfile="Cache1Hour")]        public ActionResult Index()        {            return View();        }



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn