可以寫在一個aspx網頁理(如果放在UserControl裡的話就只有那個UserControl會產生快取)
//30秒後過期,如果參數myID資料有變動時就會產生新的Cache,
//如果不用可以設成VaryByParam="none"
<%@ OutputCache Duration="30" VaryByParam="myID"%>
也可以寫在Web.Config檔裡面
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
//另外還有varyBy其他屬性的參數
<add name="mycache" duration="30" varyByParam="none">
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
如此就可以在網頁裡寫下面語句來套用
<%@ OutputCache CacheProfile="mycache"%>
又可以用程式來寫(寫在Page_Load裡面就可)
//HttpCacheability有好幾種,像是public NoCache private Server
//ServerAndNoCache ServerAndPrivate
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(20));
Response.Cache.SetValidUnitlExpires(true);