我希望某些頁面有10分鐘的客戶端緩存和24小時的服務器緩存。原因是,如果頁面更改,客戶端將在10分鐘內獲取更新的版本,但如果沒有任何更改,服務器將只需每天重新生成一次頁面。如何爲客戶端和服務器緩存設置不同的緩存過期時間
問題是輸出緩存設置似乎覆蓋客戶端設置。這裏是我的設置:
定製ActionFilterAttribute類
public class ClientCacheAttribute : ActionFilterAttribute
{
private bool _noClientCache;
public int ExpireMinutes { get; set; }
public ClientCacheAttribute(bool noClientCache)
{
_noClientCache = noClientCache;
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (_noClientCache || ExpireMinutes <= 0)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
}
else
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(ExpireMinutes));
}
base.OnResultExecuting(filterContext);
}
}
Web配置設置
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache24Hours" location="Server" duration="86400" varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
如何,我叫它:
[OutputCache(CacheProfile = "Cache24Hours")]
[ClientCacheAttribute(false, ExpireMinutes = 10)]
public class HomeController : Controller
{
[...]
}
但廁所國王在HTTP標頭顯示:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Expires: -1
我該如何正確實施這個?它是一個ASP.NET MVC 4應用程序。
我不熟悉這種緩存語法。我使用持續時間。與它取得成功。這對您的調試沒有幫助,但它的替代建議 – 2013-02-11 23:47:48