OutputCache 實作筆記
很方便實用的機制
只要在ActionResult上加上Attribute即可完成
SAMPLE-1.
Controller部分
public class HomeController : Controller
{
[OutputCache(Duration = 60)]
public ActionResult Index()
{
ViewBag.Subject = "設定Cache時間60秒";
ViewBag.Description = "Location 不設定的話,預設值為OutputCacheLocation.Any可參考( https://msdn.microsoft.com/zh-tw/library/dd492985(v=vs.118).aspx)";
ViewBag.CacheTime = DateTime.Now;
return View();
}
}
View部分
<div>
<label>@ViewBag.Subject</label>
<div>@ViewBag.Description</div>
<div>@ViewBag.CacheTime</div>
</div>
SAMPLE-2.
Controller部分
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client,Duration = 60)]
public ActionResult Index2()
{
ViewBag.Subject = "設定Cache存在Client端,時間60秒";
ViewBag.Description = "按下F5重新整理頁面就會把存在Client的OutputCache清掉,但如果先連到別的頁面再回來此頁會發現cache還在";
ViewBag.CacheTime = DateTime.Now;
return View("Index");
}
View部分都return到同一個View所以就不重複了
SAMPLE-3.
Controller部分
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Server, Duration = 60)]
public ActionResult Index3()
{
ViewBag.Subject = "設定Cache存在Server端,時間60秒";
ViewBag.Description = "參考(https://msdn.microsoft.com/zh-tw/library/system.web.ui.outputcachelocation(v=vs.118).aspx)";
ViewBag.CacheTime = DateTime.Now;
return View("Index");
}
SAMPLE-4.
Controller部分
[OutputCache(Location = System.Web.UI.OutputCacheLocation.ServerAndClient, Duration = 60)]
public ActionResult Index4()
{
ViewBag.Subject = "設定Cache存在Client端及Server端,時間60秒";
ViewBag.Description = "參考(https://msdn.microsoft.com/zh-tw/library/system.web.ui.outputcachelocation(v=vs.118).aspx)";
ViewBag.CacheTime = DateTime.Now;
return View("Index");
}
SAMPLE-5.
Controller部分
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Any, Duration = 60)]
public ActionResult Index5()
{
ViewBag.Subject = "設定Cache存在Client端及Server端及Proxy代理伺服器,時間60秒";
ViewBag.Description = "參考(https://msdn.microsoft.com/zh-tw/library/system.web.ui.outputcachelocation(v=vs.118).aspx)";
ViewBag.CacheTime = DateTime.Now;
return View("Index");
}
因為每個ActionResult都設定自己Cache的時間(Duration),假如以後60秒全部要改成120秒會有點麻煩,為了方便之後維護
加入outputCacheProfiles
Web.config部分
<configuration>
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="TenSec" enabled="true" duration="10" varyByParam="*" varyByCustom="browser" location="Any" />
<add name="OneMinute" enabled="true" duration="60" varyByParam="*" varyByCustom="browser" location="Any" />
<add name="AllPassParam" enabled="true" duration="120" varyByParam="*" varyByCustom="browser" location="Any" />
<add name="SpecifyParam" enabled="true" duration="120" varyByParam="myId,myName" varyByCustom="browser" location="Any" />
<add name="NotAllowParam" enabled="true" duration="120" varyByParam="None" varyByCustom="browser" location="Any" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
</configuration>
依照個人需求設定Cache時間(Duration)
接著只要在ActionResult上的Attribute中把CacheProfile 設定成在outputCacheProfiles內的任一個項目即可
[OutputCache(CacheProfile = "OneMinute")]
public ActionResult Index()
{
ViewBag.Subject = "透過CacheProfile集中管理使用OneMinute的設定";
ViewBag.Description = "當有兩個以上相同的時候都是參考相同的設定所以方便維護";
ViewBag.CacheTime = DateTime.Now;
return View();
}
有時候網頁時會有不同的QueryString,例如?page=2 之類的,參數不同的時後可能會需要建立不同的outputCache
才不會所有造成不管瀏覽第幾頁或是給不同參數,卻都顯示相同頁面相同內容的問題
Situation-1. 不做限制任何參數都建立一個不同版本的outputCache
[OutputCache(CacheProfile = "AllPassParam",VaryByParam ="*")]
public ActionResult Index(string x,string y, string z)
{
ViewBag.Subject = "不限制參數";
ViewBag.Description = "只要參數不同或是參數的值不同,就會建立一版不同的cache";
ViewBag.eg = string.Format("代入的參數值為:{0},{1},{2}",x,y,z);
ViewBag.CacheTime = DateTime.Now;
return View();
}
Situation-2. 指定某些參數值不同時會建立一個不同版本的outputCache
[OutputCache(CacheProfile = "SpecifyParam",VaryByParam = "myId;myName")]
public ActionResult Index2(string myId, string myName, string myAge)
{
ViewBag.Subject = "指定參數";
ViewBag.Description = "本範例設定為只有參數myId及myName的值不同時會建立不同版本的cache,參數myAge不會";
ViewBag.eg = string.Format("代入的參數值為:{0},{1},{2}", myId, myName, myAge);
ViewBag.CacheTime = DateTime.Now;
return View("Index");
}
Situation-3. 不允許任何參數,不管任何參數值不同都不會建立不同版本的outputCache
(不管參數怎麼變大家讀到的都是相同頁面相同內容)
[OutputCache(CacheProfile = "NotAllowParam",VaryByParam ="none")]
public ActionResult Index3(string myId, string myName, string myAge)
{
ViewBag.Subject = "不允許參數";
ViewBag.Description = "本範例設定為只有一個版本的cache,參數的值不同並不會建立不同版本的cache";
ViewBag.eg = string.Format("代入的參數值為:{0},{1},{2}", myId, myName, myAge);
ViewBag.CacheTime = DateTime.Now;
return View("Index");
}
在設定後發現將varyByParam參數設定在web.config疑似會問題有參考以下網址
http://stackoverflow.com/questions/23764926/asp-net-outputcache-varybyparam-does-not-work-in-web-config
範例檔: