mr.roboto
mr.roboto

Reputation: 31

Output cache not working for AJAX varyByParam

I have an endpoint for a WCF web service which is not getting output cache hits in the performance monitor for AJAX requests, it does get an output cache for non AJAX requests to endpoint.

Web config:

<caching>
  <outputCacheSettings >
    <outputCacheProfiles>
      <add location="Any" name="myCache" duration="3600" varyByParam="*" />
     </outputCacheProfiles>
  </outputCacheSettings>
</caching>

Is there an option I'm missing in the config? I've not included Javascript because I doubt the problem lies there since the server shouldn't have to check any headers to determine cache worthiness.

The endpoint:

[AspNetCacheProfileAttribute("myCache")]
        [WebGet(ResponseFormat= WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "suburb?name={name}&state={state}&category={category}&bedrooms={bedrooms}&country={country}")]

Upvotes: 3

Views: 868

Answers (1)

Jomy John
Jomy John

Reputation: 6518

When you make an ajax call , it is possible that, you are passing it as cache: false.

When you call like that, ajax call will pass the URL with an extra parameter and value will be a random unique number. Since asp.net method receive this extra parameter and you configure outputcache with varyByParam="*" , this method will never cache. The solutions are

  1. set cache: true in ajax call - (not good when you are calling a dynamic method ) or
  2. set outputcache varyByParam="none" (use only when your method need cache irrespective of parameters) or
  3. set outputcache varyByParam="parameter1;parameter2" (this is the ideal solution if you have multiple parameters) . For single parameter use varyByParam="parameter1"

Sorry for the late reply. I saw this question just now.

Upvotes: 1

Related Questions