簡體   English   中英

自定義輸出緩存mvc4

[英]Custom output cache mvc4

我正在嘗試在MVC4項目上利用緩存,並且我在主頁上設置了以下屬性:

[OutputCache(Location=OutputCacheLocation.ServerAndClient,Duration=14400)]

效果很好,但是持續時間給我帶來了問題。 我需要的是緩存在新的一天(每天午夜)開始時過期。 我可以將持續時間設置為24小時,但是這不能解決我的問題,因為每天的開始我的頁面都有新內容。 我已經研究了variant by param方法,並且我知道可以將日期附加到URL,但這很混亂。 有人知道替代品嗎?

提前致謝

一種解決方案是擴展OutputCacheAttribute並創建一個適用於午夜的新方法,因為您可以在構造函數中設置Duration

public class OutputCacheMidnightAttribute : OutputCacheAttribute
{
    public OutputCacheMidnightAttribute()
    {
        // remaining time to midnight
        Duration = (int)((new TimeSpan(24, 0, 0)) - DateTime.Now.TimeOfDay).TotalSeconds;
    }
}

你這樣使用它

[OutputCacheMidnight(Location=OutputCacheLocation.ServerAndClient)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM