將頁面輸出存入記憶體,後續請求直接回傳快取內容,跳過頁面初始化、載入、渲染等處理,大幅降低伺服器資源消耗。
新聞網頁數小時不變、大量用戶同時請求同一頁面 → 無需重複執行代碼生成相同 HTML。
<%@ OutputCache Duration="#ofseconds"
Location="Any | Client | Downstream | Server | None | ServerAndClient"
Shared="True | False"
VaryByControl="controlname"
VaryByCustom="browser | customstring"
VaryByHeader="headers"
VaryByParam="parametername"
VaryByContentEncoding="encodings"
CacheProfile="cache profile name | ''"
NoStore="true | false"
SqlDependency="database/table name pair | CommandNotification"
%>
Duration:快取秒數VaryByCustom:依瀏覽器類型或自訂字串變化VaryByParam:依查詢字串/表單參數變化VaryByControl:依使用者控制項屬性變化其餘屬性請參考 MSDN。演示中用 DateTime.Now 顯示於 lblResult 驗證快取生效與否。