Simon
Simon

Reputation: 2116

MVC OutputCache still allowing my controller to be hit on 2nd call

I have the controller action below which is called via $.ajax

[OutputCache(Duration = 60, Location = OutputCacheLocation.Client, NoStore = true, VaryByParam = "*")]
public JsonResult GetSomeViewModel(int id, string searchDate)

I have a breakpoint within that action.

Now I would expect on the 2nd time of calling(within 60 seconds at least) that my breakpoint wouldn't get hit as the browser should have cached that response.

This is the first time I have tried to use this functionality.

What am i doing wrong?

This is happening on all major browsers.

Upvotes: 1

Views: 756

Answers (2)

gregsonian
gregsonian

Reputation: 1216

I too ran into this problem. I was using $.ajax to asynchronosuly load content from an RSS feed. After configuring the $.ajax call with cache: true (yes, true, to disable cache-busting; see here for details.) I was still getting into the Action on subsequent requests.

My hypothesis is there exists a threshold that prevents OutputCache from activating. My research lead me here (subsection: Caching Intricacies) and these settings may be affecting when OutputCache is actually getting used. My basic tests seem to confirm that only the first two requests get into the action but 3 or more (within the time period of ten seconds) does not.

I would be curious if anyone else has been able to more extensively test the OutputCacheAttribute and can either corroborate or refute my findings.

Upvotes: 0

Steven V
Steven V

Reputation: 16585

You mention using $.ajax, which is sounds like jQuery. By default, jQuery will send the request with a cache buster attached. If you monitor the network traffic you'll probably see something like _=1406209313719 in the query string of you ajax request. The integer is a timestamp, so it will always change and will work around most browser caching.

You can set jQuery to not use the cache buster by setting cache to false on your ajax method.

$.ajax({
    cache: false
});

Upvotes: 4

Related Questions