OutputCache忽略时间戳

时间:2015-05-26 13:33:11

标签: asp.net-mvc outputcache

我在MVC中使用OutputCache但是当时间戳附加到URL时我很难缓存。

特别是jQuery的AJAX组件将_ = 21321423432添加到URL的末尾。

每当我调用操作的URL时,它总是会进入操作而不是返回缓存的项目。

我正在使用VarByCustom& VarybyParam元素根据登录用户以及开始和结束日期进行缓存。

动作

    [OutputCache(CacheProfile = "FeedCache")]
    public async Task<JsonResult> Feed(DateTime start, DateTime end)
    {
        //Do something

        return Json(result, JsonRequestBehavior.AllowGet);
    }

配置

<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);
}

我的印象是这会专门为用户缓存而且然而,开始和结束参数并非如此。有什么我想念的吗?

2 个答案:

答案 0 :(得分:0)

jQuery's AJAX component appends a "_={timestamp}" only when you set cache option to false or for dataTypes 'script' and 'jsonp'您可能会返回一组JSON对象,因此请尝试将cache:true添加到您的ajax选项中。它应该类似于:

$.ajax({
        type: ...,
        url: ...,        
        cache: true,
        success: function (data) {
        }
    });

答案 1 :(得分:0)

如果我在属性中指定OutputCache详细信息而不是webconfig中的cacheprofile,那么它可以工作。

<强>作品

[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" />

我真的很想知道为什么他们有不同的行为......