0

I'm using the OutputCache in MVC but I'm having difficulty caching when a timestamp has been appended to the URL.

In particular jQuery's AJAX component adds _=21321423432 to the end of the url.

Everytime I call the action's URL it will always step into the action instead of returning the cached item.

I'm using the VarByCustom & VarybyParam elements to cache based on the logged in user and the start and end dates.

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

I was under the impression this would cache specifically for the user & the start and end parameters however this is not the case. Is there something I'm missing?

heymega
  • 9,215
  • 8
  • 42
  • 61

2 Answers2

0

jQuery's AJAX component appends a "_={timestamp}" only when you set cache option to false or for dataTypes 'script' and 'jsonp' You probably return an array of JSON objects so try to add cache:true to your ajax options. It should be similar to:

$.ajax({
        type: ...,
        url: ...,        
        cache: true,
        success: function (data) {
        }
    });
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
  • Thank you for your answer - Unfortunately I can't risk the data being cached by the browser as it contains user data. – heymega May 26 '15 at 13:47
  • This wont risk any data this simply passes the cache control to the server. So if you have OutputCache set it will be responsible for caching strategy – Alex Art. May 26 '15 at 14:03
  • Browsers cache GET requests so the call would never be made to the server. – heymega May 26 '15 at 14:10
  • No if you specify [OutputCache(Location = OutputCacheLocation.Server)] – Alex Art. May 26 '15 at 14:14
  • If you set cache:true the browser will cache the request. Next time you make that call the browser will return the response from the cache and will not call the server. Therefore, the servers cache will never be touched. Does that make sense? – heymega May 26 '15 at 14:17
  • No :-). Just take a look at the http://api.jquery.com/jquery.ajax/ and search for cache option – Alex Art. May 26 '15 at 14:19
0

If I specify the OutputCache details within the attribute instead of as a cacheprofile in the webconfig it works.

Works

[OutputCache(Duration = 600, Location = OutputCacheLocation.Server, VaryByParam = "start;end", VaryByCustom = "user")]

Doesn't Work

[OutputCache(CacheProfile = "FeedCache")]

<add name="FeedCache" duration="600" varyByParam="start;end" varyByCustom="user" location="Server" />

I'd really like to know why they have different behaviours...

heymega
  • 9,215
  • 8
  • 42
  • 61