MVC页面缓存
1.OutputCache 属性 contact.cshtml
[OutputCache(Duration=10)]
public
ActionResult Contact()
{
ViewBag.Message =
"Your contact page."
;
return
View();
}
action 结果会缓存10秒钟
2.带参数的OutputCache
[OutputCache(Duration=3600, VaryByParam=
"searchTerm"
)]
public
ActionResult Contact(
string
searchTerm)
3.缓存child action partialviewtest.cshtml
<
p
>
This is a partial view.
</
p
>
public
ActionResult PartialViewTest()
{
return
View();
}
contact.cshtml中,加入如下代码@Html.Action("PartialViewTest"
)
在partialviewtest action上加入[OutputCache(Duration=3600)]设置,partialviewtest的缓存时间不会受contact action缓存时间的影响
4 指定缓存位置Specify the cache location
[OutputCache(Duration = 3600, Location=System.Web.UI.OutputCacheLocation.ServerAndClient)]
默认值是serverandclient
5.根据header值进行缓存
例如多区域语言情况下
[OutputCache(Duration = 3600, VaryByHeader="Accept-Language"
)],可以根据不同的语言进行缓存。
other type of header value will be important for AJAX requests. Let’s imagine that the Contact action is also available to AJAX calls. In other words only that portion of the screen will be refreshed which makes up the View result of the Contact action. The rest, i.e. the header, footer, menu etc. will stay constant. If a user then bookmarks a specific search result, e.g.:
http://localhost:xxxx/Home/Contact?searchTerm=a
…and will call this search directly from his/her browser then the Contact action will only serve up a cached version of the Contact view and nothing else. The reason is that user’s browser will send a full GET request and not an asynchronous one. All the other parts of the ‘normal’ full screen, i.e. everything that the AJAX call didn’t touch will be left out. The result will be very weird: a page devoid of styles – apart from any inline style tags in the Contact view – headers, footers etc. The problem lies with a specific header value: X-Requested-With. This is the only difference between the headers of the AJAX and the GET calls: it will be present on an AJAX request but absent on GET requests. To the caching engine there’s no difference between AJAX calls and GET calls because you didn’t specify this in the OutputCache attribute. Two things need to be done:
1. Update the OutputCache attribute to vary the output by this specific header type:
1
|
[OutputCache(Duration = 3600, VaryByHeader= "X-Requested-With" )] |
Now the caching mechanism will be able to differentiate between two requests: one that wants a full page render and one that will only serve a portion of the page.
Unfortunately there are cases when this update is not enough. The default setting is that we’re letting the action result be cached on both the client and the server. However, some older browsers are not “clever” enough to detect the difference between full GET and partial AJAX requests.
2. To be on the safe side we need to specify that caching only should occur on the server:
You can achieve this as follows:
1
|
[OutputCache(Duration = 3600, VaryByHeader= "X-Requested-With" , Location=System.Web.UI.OutputCacheLocation.Server)] |
Now the server will send instructions to the browser not to cache the action result. The browser will always have to access the server for a response. The server will then serve up a cached result if there’s any in its memory.
The safest and most elegant solution is of course to create separate action methods for full GET and partial AJAX calls
<
caching
>
<
outputCacheSettings
>
<
outputCacheProfiles
>
<
add
name
=
"Aggressive"
duration
=
"1600"
/>
<
add
name
=
"Short"
duration
=
"20"
/>
</
outputCacheProfiles
>
</
outputCacheSettings
>
</
caching
>
可以这样使用:
[OutputCache(CacheProfile="Aggressive")]