我在MVC中使用OutputCache,但是当时间戳被附加到URL时,我很难缓存。
特别是jQuery的AJAX组件将_=21321423432添加到url的末尾。
每次我调用该操作的URL时,它总是进入该操作,而不是返回缓存的项。
我使用VarByCustom & VarybyParam元素根据登录用户以及开始日期和结束日期进行缓存。
Action
[OutputCache(CacheProfile = "FeedCache")]
public async Task<JsonResult> Feed(DateTime start, DateTime end)
{
//Do something
return Json(result, JsonRequestBehavior.AllowGet);
}Config
<add name="FeedCache" duration="600" varyByParam="start;end" varyByCustom="user" location="Server" />Global.asmx
public override string GetVaryByCustomString(HttpContext context, string custom)
{
//
// Vary by logged in user
//
if (custom == "user")
{
return "user=" + context.User.Identity.Name;
}
return base.GetVaryByCustomString(context, custom);
}我当时的印象是,这将专门为用户缓存&起始参数和结束参数,但事实并非如此。我遗漏了什么吗?
发布于 2015-05-27 10:54:49
如果我在属性中指定OutputCache详细信息,而不是将其作为webconfig中的缓存配置文件,它就能工作。
Works
[OutputCache(Duration = 600, Location = OutputCacheLocation.Server, VaryByParam = "start;end", VaryByCustom = "user")]不工作
[OutputCache(CacheProfile = "FeedCache")]
<add name="FeedCache" duration="600" varyByParam="start;end" varyByCustom="user" location="Server" />我真的很想知道他们为什么有不同的行为..。
发布于 2015-05-26 13:42:24
or for dataTypes 'script' and 'jsonp'您可能返回一个JSON对象数组,因此尝试将cache:true添加到ajax选项中。它应类似于:
$.ajax({
type: ...,
url: ...,
cache: true,
success: function (data) {
}
});https://stackoverflow.com/questions/30460087
复制相似问题