2011-06-14 80 views
0

我正在开发一个内部网,我通过jQuery每隔x秒刷新一次PartialView(3用于调试目的)。同时,我在PartialView操作中使用[OutputCaching(Duration = 3)]指令。在ASP.NET MVC 3中禁用ChildAction上的服务器端输出缓存,或者因客户端而异

用于提神的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刷新时都会加载partialview的缓存版本。我可以更改哪个版本的partialview被缓存的唯一方法是直接使用/_News/Home/ 加载partialview ChildActions不允许NoStore属性,所以这也不是解决方案。

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

回答

1

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

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

我已经试过了,位置和NoStore一样,不允许儿童操作,但是谢谢你的建议 – Casper 2011-06-16 14:20:43

0

你会想要利用输出缓存的VaryByCustom attribute。这将允许您为每个用户(或您确定的其他唯一缓存键)拥有唯一的缓存版本。

相关问题