0


几个小时以来一直在努力解决这个问题。这是我要解决的问题:

我有这个使用 CacheProfile 的控制器/动作:

    [DonutOutputCache(CacheProfile = "CachedAction")]
    [ChildActionOnly]
    public ActionResult ListOrders(string id, string selectedOrders)
    {
    }

这是我的 web.config 设置:

    <caching>
    <outputCache enableOutputCache="true" />
    <outputCacheSettings>
        <outputCacheProfiles>
            <add name="CachedAction" duration="14100" varyByParam="id;selectedOrders" location="Any" />
        </outputCacheProfiles>
    </outputCacheSettings>

到目前为止一切正常,缓存按预期工作!

问题出在我的页面上,我有一个小“刷新按钮”,用户可以单击它来获取最新数据。为此,我只是在用户点击刷新后从页面调用 $.ajax(),但我调用了另一个操作,因为如果我调用原始 ListOrders,我只会得到它的缓存副本。

    $.ajax({
        url: '/controller/myajaxrefreshaorders/1?selectedOrders=xxxx',
        type: "GET",
        async:true,
        cache: false,

这是我的问题。如果您看到我只是想破坏缓存并重定向到原始操作,它应该只是返回最新数据并更新缓存。但不管我做什么,它都不起作用!!!

public ActionResult MyAjaxRefreshOrders(string id, string selectedOrders)
    {
        var Ocm = new OutputCacheManager();
        Ocm.RemoveItem("Controller", "ListOrders", new { id = id, selectedOrders= selectedOrders });
        Response.RemoveOutputCacheItem(Url.Action("ListOrders", "Controller", new { id = id, selectedOrders = selectedOrders }));


        return RedirectToAction("ListOrders", new { id = id, selectedOrders = selectedOrders });
    }

事实上,这是我对现实中发生的事情的观察:

  1. 如果我继续重新加载页面,缓存工作正常,它显示最后一次检索项目的时间戳,这很棒。
  2. 如果我点击那个 ajaxrefresh 按钮,它会转到服务器,通过我的 cachebust 代码并简单地返回......即返回 RedirectToAction("ListOrders") 的调用永远不会进入该函数。
  3. 最后,ajaxcall 似乎为我创建了另一个缓存版本的操作。因此,在 ajax 调用完成后显示的时间戳是不同的时间戳,而当我重新加载页面时显示的时间戳也不同。

任何人有任何想法我做错了什么?我将非常感谢您的帮助,因为这让我发疯了!

4

2 回答 2

1
//  Get the url for the action method:
var staleItem = Url.Action("Action", "YourController", new
{
    Id = model.Id,
    area = "areaname";
});

//  Remove the item from cache
Response.RemoveOutputCacheItem(staleItem);

此外,您需要记住将 Location=OutputCacheLocation.Server 参数添加到 OutputCache 属性,如下所示:

[OutputCache(Location=System.Web.UI.OutputCacheLocation.Server, Duration = 300, VaryByParam = "Id")]
于 2015-06-25T07:53:40.847 回答
0

回答我自己的问题。看起来这是 DonutCache 中的一个错误。对我有用的是这段代码。(所以基本上,我使用了 RemoveItems 而不是 RemoveItem)。疯狂!!!

    var Ocm = new OutputCacheManager();
    RouteValueDictionary rv = new RouteValueDictionary();
    rv.Add("id", id);
    rv.Add("selectedorders", selectedOrders);
    Ocm.RemoveItems("controller", "listorders", rv);

不过需要注意的是,由于某种原因,MVC 中的 RedirectToAction() 会将旧的缓存副本返回给客户端。不确定是 Chrome 搞砸了我还是 MVC。我怀疑是 Chrome 弄乱了 302 重定向(即使我使用的是 $.ajax(cache:false)。对我来说,解决方法是先调用 methodA(BustCache),然后再调用 MVC Action 以获得新鲜数据。

于 2014-07-27T16:47:16.730 回答