在ASP.NET MVC 3中禁用ChildActions上的服务器端输出缓存或因客户端而异

时间:2011-06-14 12:23:36

标签: asp.net-mvc-3 caching outputcache

我正在开发一个内部网,我通过jQuery每隔x秒(3个用于调试目的)刷新一次。同时,我在PartialView操作上使用[OutputCaching(Duration = 3)]指令。

用于刷新的javascript如下:

<script type="text/javascript">
    $(document).ready(function () {
        setInterval(function () {
            $("#partial_1").load('@Url.Action("_News", "Home", new { id = 1 })');
        }, 3000);
    });
</script>

_News行动目前如下:

    [OutputCache(Duration=3)]
    public ActionResult _News(int id)
    {
        /* random stuff to get news AND the reason why it should be on per-client-basis
           is that this also contains logic for authentification
        */
        return PartialView();
    }

只要只有一个用户,这一切都可以。

当多个用户访问该页面时会出现问题,因为所有用户都看到相同的版本。

完全禁用此操作的输出缓存可行,但只是删除[OutputCaching]指令不起作用。如果我这样做,每次jQuery刷新发生时都会加载部分视图的缓存版本。我可以更改缓存部分视图的哪个版本的唯一方法是使用/_News/Home/直接加载partialview ChildActions不允许使用NoStore属性,因此这也不是解决方案。

我考虑过使用VaryByParam属性,但我不确定如何根据用户的不同来实现这一点,在这种情况下User.Identity就足够了。

2 个答案:

答案 0 :(得分:1)

您可以尝试将缓存位置强制到客户端,以便每个用户(客户端)都有自己的缓存版本。

[OutputCache(Duration=3, Location=OutputCacheLocation.Client)]

答案 1 :(得分:0)

您需要使用输出缓存的VaryByCustom attribute。这将允许您为每个用户(或您确定的其他一些唯一缓存键)提供唯一的缓存版本。

相关问题