我刚开始研究缓存以提高性能,并有一个关于用于 AJAX 调用的缓存的问题。
我有一个操作,用于查询 Twitter 并返回结果。目前,当用户按下按钮时,它会加载一个旋转的 gif,同时跳转到该操作以执行查询,然后返回部分视图。然后 jQuery 会使用 HTML 响应更新 div。通常这需要大约 5 秒钟。然后他们有一个“更多”按钮,可以获取更多结果。
如果我在此操作上放置 CachingAttribute,会发生什么?我知道我可以尝试一下,但我只是想解释一下技术方面的事情。
谢谢
以下是我的 Javascript:
我有一个操作,用于查询 Twitter 并返回结果。目前,当用户按下按钮时,它会加载一个旋转的 gif,同时跳转到该操作以执行查询,然后返回部分视图。然后 jQuery 会使用 HTML 响应更新 div。通常这需要大约 5 秒钟。然后他们有一个“更多”按钮,可以获取更多结果。
如果我在此操作上放置 CachingAttribute,会发生什么?我知道我可以尝试一下,但我只是想解释一下技术方面的事情。
谢谢
以下是我的 Javascript:
$('#blogEntryList #moreLink').live("click", function() {
$('#morespan').toggle();
$('#loader').toggle();
$.get($(this).attr("href"), function(response) {
$('#blogEntryList ol').append($("ol", response).html());
$('#blogEntryList #moreLink').replaceWith($("#moreLink", response));
$('#loader').hide();
$('#morespan').show();
});
return false;
});
这是我修改后的Action:
[OutputCache(
Location = OutputCacheLocation.Server,
Duration = 100,
VaryByParam = "")]
public ActionResult BlogPosts(int? entryCount)
{
if (!entryCount.HasValue)
entryCount = defaultEntryCount;
int page = entryCount.Value / defaultEntryCount;
IEnumerable<BlogData> pagedEntries = GetLatestEntries(page, defaultEntryCount);
if (entryCount < totalItems)
AddMoreUrlToViewData(entryCount.Value);
return View("BlogEntries", pagedEntries);
}
VaryByParam="PageNumber;StartPage;Foo;Bar"
或者如果您希望缓存根据每个操作参数组合而有所不同:VaryByParam="*"
- Darin Dimitrov