ASP MVC 3 OutputCache避免缓存当前请求

发布于 2025-01-03 02:30:58 字数 581 浏览 3 评论 0原文

我开始在我的网站上使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2025-01-10 02:30:58

您所描述的内容有时被称为“甜甜圈洞缓存”,因为您想要缓存除了中间的一些动态内容之外的所有内容。

以下是您可能需要查看的一些资源:

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:

护你周全 2025-01-10 02:30:58

我不知道是否有一种简单的方法可以满足您的要求。但是,我对消息和 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.

盗琴音 2025-01-10 02:30:58

那这个呢?

class CustomOutputCacheAttribute : OutputCacheAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Response.StatusCode >= 400)
        {
            this.Location = System.Web.UI.OutputCacheLocation.None;
            this.Duration = -1;
        }

        filterContext.HttpContext.Response.Cache.SetOmitVaryStar(true);
        base.OnActionExecuted(filterContext);
    }

}

what about this?

class CustomOutputCacheAttribute : OutputCacheAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Response.StatusCode >= 400)
        {
            this.Location = System.Web.UI.OutputCacheLocation.None;
            this.Duration = -1;
        }

        filterContext.HttpContext.Response.Cache.SetOmitVaryStar(true);
        base.OnActionExecuted(filterContext);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文