Heim  >  Artikel  >  Web-Frontend  >  MVC3缓存(一):页面缓存_html/css_WEB-ITnose

MVC3缓存(一):页面缓存_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:53:181224Durchsuche

启用页面缓存


在MVC3中如果要启用页面缓存,需要在页面对应的Action前面加上一个OutputCache属性。

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

在Controller中添加对应的Action,并加上OutputCache属性。

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

刷新页面即可看到页面做了一个10s的缓存。当页面中数据不是需要时时的呈现给用户时,这样的页面缓存可以减小时时的对数据处理和请求,当然这是针对整个页面做的缓存,缓存的力度还是比较粗的。


缓存的位置


可以通过设置缓存的Location属性,决定将缓存放置在何处。

Location可以设置的属性为(Any   Client   Downstream   Server   None   ServerAndClient)

Location的默认值为Any。一般推荐将用户的信息存储在Client端,一些公用的信息存储在Server端。

加上Location应该是这样的。

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

配置文件中通用设置


当我们需要对多个Action进行统一的设置时,可以在web.config文件中统一配置后进行应用即可。

在web.config中配置下Caching节点

<caching><outputcachesettings>    <outputcacheprofiles>        <add name="Cache1Hour" duration="3600" varybyparam="none"></add>    </outputcacheprofiles></outputcachesettings></caching>

那么在Action上使用该配置节点即可,这样的方法对于统一管理配置信息比较方便。

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



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn