Jos*_*osh 13 asp.net asp.net-mvc caching outputcache
我希望某些页面的客户端有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);
}
}
Run Code Online (Sandbox Code Playgroud)
Web配置设置
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache24Hours" location="Server" duration="86400" varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它:
[OutputCache(CacheProfile = "Cache24Hours")]
[ClientCacheAttribute(false, ExpireMinutes = 10)]
public class HomeController : Controller
{
[...]
}
Run Code Online (Sandbox Code Playgroud)
但是看看HTTP Header显示:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Expires: -1
Run Code Online (Sandbox Code Playgroud)
我该如何正确实现?它是一个ASP.NET MVC 4应用程序.
您需要为服务器端缓存和客户端缓存实现自己的解决方案,或者使用ClientCacheAttribute或OutputCache.以下是您需要自定义服务器端缓存解决方案的原因.
这里我要强调的是 we don't have collection of HttpCachePolicyBase but we only have one object of HttpCachePolicyBase so we can't set multiple cache policy for given response.
即使我们可以将Http Cacheability设置为HttpCacheability.ServerAndPrivate但又会在其他问题中运行缓存持续时间(即客户端10分钟和服务器24小时)
我建议使用OutputCache进行客户端缓存,并为服务器端缓存实现自己的缓存机制.
归档时间: |
|
查看次数: |
5975 次 |
最近记录: |