ASP MVC 3 OutputCache避免缓存当前请求
我开始在我的网站上使用 OutputCache。 我遇到的问题是,当用户更新项目时,我需要重置该项目的缓存。
我使用以下方法做到了这一点:
var urlToRemove = Url.Action("Details", "Dress", new {id = model.Id});
Response.RemoveOutputCacheItem(urlToRemove);
在“编辑”操作中,我还将更新成功消息设置为 TempData,并在下一个请求时显示它。问题在于该消息保留在缓存的响应中。
你知道我怎样才能避免在动作中缓存吗?比如:
[OutputCache(Duration = 3600, VaryByParam = "id")]
public ViewResult Details(int id)
{
if(NotificationHelper.HasNotifications)
Response.DoNotCache();
.....
我不能使用同样的技巧......因为页面在渲染后被添加到缓存中。所以我无法从其主体的缓存中排除某个操作。
I started using OutputCache for my website.
The problem that I encounter is that when a user update an item I need to reset the cache for that item.
I did that using:
var urlToRemove = Url.Action("Details", "Dress", new {id = model.Id});
Response.RemoveOutputCacheItem(urlToRemove);
In Edit action I also set to TempData the update success message and I display it on the next request. The problem is that the message remains in the cached response.
Do you know how can I avoid caching in an action. Something like:
[OutputCache(Duration = 3600, VaryByParam = "id")]
public ViewResult Details(int id)
{
if(NotificationHelper.HasNotifications)
Response.DoNotCache();
.....
I cannot use the same trick ... because the page is added to the cache after its rendered. So I cannot exclude an action from cache in its body.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所描述的内容有时被称为“甜甜圈洞缓存”,因为您想要缓存除了中间的一些动态内容之外的所有内容。
以下是您可能需要查看的一些资源:
What you are describing is sometimes referred as "Donut Hole Caching" because you want to cache everything except some bit of dynamic content in the middle.
Here is are a couple resources you might want to look at:
我不知道是否有一种简单的方法可以满足您的要求。但是,我对消息和 OutputCache 的经验是不要将它们放入响应中。我最终确保缓存页面上没有显示任何消息。如果我绝对必须在缓存页面上有一条消息,我会设置一个 ajax 调用,一旦响应到达客户端就会抓取消息。
I don't know if there is an easy way to do what you are asking. However, my experience with messages and OutputCache is don't put them in the response. I ended up making sure there were no messages that are ever displayed on a cached page. If I absolutely had to have a message on a cached page I would set up an ajax call that would grab the messages once the response reached the client.
那这个呢?
what about this?