我正在开发一个intranet,其中我有一个PartialView通过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操作如下(_N):
[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/
加载部分视图ChildActions不允许使用NoStore属性,因此这也不是解决方案。
我考虑过使用VaryByParam属性,但我不确定如何实现这一点,以根据用户的不同而不同,在这种情况下,user.Identity就足够了。
您可以尝试将缓存位置强制到客户端,这样每个用户(客户端)都有自己的缓存版本。
[OutputCache(Duration=3, Location=OutputCacheLocation.Client)]
您将希望利用输出缓存的VaryByCustom属性。这将允许您为每个用户拥有一个唯一的缓存版本(或您确定的其他一些唯一缓存密钥)。