【问题标题】:Patterns for caching/lookup of MVC partial views on client在客户端缓存/查找 MVC 部分视图的模式
【发布时间】:2012-07-27 19:27:58
【问题描述】:

这是我的场景。我正在调用一个指向返回部分视图的控制器方法的 url:

控制器方法:

public ActionResult UserProfile() { return View("UserProfile"); }

Ajax 请求获取视图:

$.get('/Home/UserProfile', function (data) { $('.content').html(data); });

我想缓存我的“UserProfile”视图,这样每次用户点击它时,它就不必再次返回服务器从控制器获取视图。

我还希望能够在从服务器获取视图之前确定视图是否已缓存在客户端上,如果有,请将其从缓存中取出并简单地将其注入到我的布局中的 div 中。

有人做过这样的事吗?

【问题讨论】:

  • 如果你实现这样的事情,不要忘记在注销(或超时等)时清除缓存

标签: asp.net-mvc-3 caching jquery asp.net-mvc-partialview


【解决方案1】:

您可以使用[OutputCache] 属性。它允许您指定要缓存视图的持续时间以及缓存的位置。例如,假设您想在客户端浏览器上缓存控制器操作的结果 30 秒:

[OutputCache(Duration = 30, Location = OutputCacheLocation.Client)]
public ActionResult UserProfile() 
{ 
    return PartialView(); 
}

现在您可以触发许多 AJAX 请求,但 30 秒内只有一个会发送到服务器:

window.setInterval(function () {
    $.get('/Home/UserProfile', function (data) {
        $('.content').html(data);
    });
}, 4000);

【讨论】:

    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多