Tôi muốn có một số trang nhất định có bộ nhớ Cache 10 phút cho khách hàng và 24 giờ cho máy chủ. Lý do là nếu trang thay đổi, khách hàng sẽ tìm nạp phiên bản cập nhật trong vòng 10 phút, nhưng nếu không có gì thay đổi thì máy chủ sẽ chỉ phải xây dựng lại trang một lần mỗi ngày.Cách đặt thời gian hết hạn Cache cho Máy khách và Máy chủ lưu trữ
Vấn đề là cài đặt bộ đệm đầu ra dường như ghi đè cài đặt ứng dụng khách. Dưới đây là những gì tôi đã thiết lập:
Tuỳ chỉnh ActionFilterAttribute Lớp
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 Config cài đặt
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache24Hours" location="Server" duration="86400" varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
Làm thế nào tôi gọi đó là:
[OutputCache(CacheProfile = "Cache24Hours")]
[ClientCacheAttribute(false, ExpireMinutes = 10)]
public class HomeController : Controller
{
[...]
}
Nhưng loo vua tại Tiêu đề HTTP hiển thị:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Expires: -1
Làm cách nào để triển khai đúng cách? Nó là một ứng dụng ASP.NET MVC 4.
Tôi không quen thuộc với cú pháp này để lưu vào bộ nhớ cache. Tôi sử dụng thời lượng. Đã thành công với nó. Điều đó không giúp gỡ lỗi của bạn, nhưng đề xuất thay thế –