I'm just starting to look in to caching to improve performance and have a question about caching for an AJAX call.
我刚刚开始研究缓存以提高性能,并对缓存AJAX调用提出疑问。
I have a action which is used to query twitter and then return the results. At the moment when a user presses a button it loads a rotating gif whilst it goes off to the action to do the query and then return a partial view. jQuery then updates a div with the HTML response from the view. Normally this takes around 5 secs. They then have a more button which goes off to get more results.
我有一个动作,用于查询twitter,然后返回结果。在用户按下按钮的时刻,它加载旋转的gif,同时进入动作以进行查询,然后返回局部视图。然后jQuery使用视图中的HTML响应更新div。通常这需要大约5秒。然后他们有一个更多的按钮,以获得更多的结果。
What will happen if I put the CachingAttribute over this action? I know I can try it but I just want the technical side of things explained.
如果我将CachingAttribute放在此操作上会发生什么?我知道我可以尝试但我只是想解释事情的技术方面。
Thanks
谢谢
Here is my Javascript:
这是我的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;
});
Here is my modified 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);
}
1 个解决方案
#1
11
Here's how it works: assuming no caching specified on the server side, by default GET requests will be cached by the browser and POST requests not cached unless you specify the cache: true
attribute when sending the AJAX requests which allows you to override the client caching strategy.
以下是它的工作原理:假设在服务器端没有指定缓存,默认情况下,GET请求将由浏览器缓存,POST请求不缓存,除非您在发送允许覆盖客户端缓存的AJAX请求时指定cache:true属性战略。
Now on the server side you could decorate your controller action with the [OutputCache]
which will allow you to define different caching strategies. You could keep a cache on the server, on downstream proxy servers, or on the client. You could also manage different expiration policies.
现在在服务器端,您可以使用[OutputCache]修饰控制器操作,这将允许您定义不同的缓存策略。您可以在服务器,下游代理服务器或客户端上保留缓存。您还可以管理不同的过期策略。
So let's illustrate this by an example:
让我们通过一个例子来说明这一点:
[OutputCache(
Location = OutputCacheLocation.Server,
Duration = 10,
VaryByParam = "")]
public ActionResult Hello()
{
return Content(DateTime.Now.ToLongTimeString(), "text/plain");
}
And on the client side:
在客户端:
$.ajax({
url: '/home/hello',
type: 'post',
success: function (result) {
alert(result);
}
});
The result of this controller action will be cached on the server for 10 seconds. This means that the server will be hit on each request but the action won't be executed if there's a cached version and will directly served from this cache. 10 seconds later from the first request which hit the controller action the cache will expire and the same process repeats.
此控制器操作的结果将缓存在服务器上10秒钟。这意味着服务器将在每个请求中被命中,但如果存在缓存版本,则不会执行该操作,并且将直接从此缓存提供服务。在第一个请求达到控制器操作的10秒后,缓存将过期并重复相同的过程。
#1
11
Here's how it works: assuming no caching specified on the server side, by default GET requests will be cached by the browser and POST requests not cached unless you specify the cache: true
attribute when sending the AJAX requests which allows you to override the client caching strategy.
以下是它的工作原理:假设在服务器端没有指定缓存,默认情况下,GET请求将由浏览器缓存,POST请求不缓存,除非您在发送允许覆盖客户端缓存的AJAX请求时指定cache:true属性战略。
Now on the server side you could decorate your controller action with the [OutputCache]
which will allow you to define different caching strategies. You could keep a cache on the server, on downstream proxy servers, or on the client. You could also manage different expiration policies.
现在在服务器端,您可以使用[OutputCache]修饰控制器操作,这将允许您定义不同的缓存策略。您可以在服务器,下游代理服务器或客户端上保留缓存。您还可以管理不同的过期策略。
So let's illustrate this by an example:
让我们通过一个例子来说明这一点:
[OutputCache(
Location = OutputCacheLocation.Server,
Duration = 10,
VaryByParam = "")]
public ActionResult Hello()
{
return Content(DateTime.Now.ToLongTimeString(), "text/plain");
}
And on the client side:
在客户端:
$.ajax({
url: '/home/hello',
type: 'post',
success: function (result) {
alert(result);
}
});
The result of this controller action will be cached on the server for 10 seconds. This means that the server will be hit on each request but the action won't be executed if there's a cached version and will directly served from this cache. 10 seconds later from the first request which hit the controller action the cache will expire and the same process repeats.
此控制器操作的结果将缓存在服务器上10秒钟。这意味着服务器将在每个请求中被命中,但如果存在缓存版本,则不会执行该操作,并且将直接从此缓存提供服务。在第一个请求达到控制器操作的10秒后,缓存将过期并重复相同的过程。