Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Response Cache not working when i use uri with query param

I am trying with response cache to cache the entire response from target and it's working only if i cache the full response but my requirement is query param if the query param value change request has to go to target and get the response but it is not going to target .

eg : /crew/roster/v1/SearchMaxPublishDatesByGrade?CrewType=A

the query param value change from CrewType=A to CrewType=C the response  i am getting rom CrewType=A not from CrewType=C.

<ResponseCache name="RC-SearchMaxPublishDatesByGrade">
  <DisplayName>RC-SearchMaxPublishDatesByGrade</DisplayName>
  <CacheResource>SearchMaxPublishDatesByGrade</CacheResource>
  <CacheKey>
    <KeyFragment ref="request.uri" type="string"/>
    <KeyFragment ref="request.queryparam.CrewType"/>
  </CacheKey>
  <Scope>Exclusive</Scope>
  <ExpirySettings>
    <TimeoutInSec>300</TimeoutInSec>
  </ExpirySettings>
</ResponseCache>
0 3 214
3 REPLIES 3

This policy configuration works for me:

<ResponseCache name="Cache-Response">
  <CacheKey>
    <KeyFragment ref='request.uri' />
  </CacheKey>
  <Scope>Application</Scope>
  <ExpirySettings>
    <TimeoutInSeconds>60</TimeoutInSeconds>
  </ExpirySettings>
  <ExcludeErrorResponse/>
  <SkipCacheLookup>request.verb != "GET"</SkipCacheLookup>
  <SkipCachePopulation>request.verb != "GET"</SkipCachePopulation>
</ResponseCache>

The request.uri variable (info on the variables reference page) includes the path as well as the query param string. So if you want to cache by queryparam, use request.uri. 

You need to attach the Responsecache policy twice: once in the ProxyEndpoint Request Preflow, and another time in the TargetEndpoint Response postflow. 

See this 5-minute screencast for a demonstration. 

Thanks for the quick response @dchiesa1 

The policy configuration is working for me as well for the same query param value eg : ?CrewType=A

and my requirement is if the param value change from ?CrewType=A to ?CrewType=C then the request has to go to target and get the response but using above configuration request not going to target instead getting response from cache till the cache TimeoutInSecond which is incorrect.

can you please guide me how i can overcome this issue.

Glad to help.  I'm sorry you're still having trouble. I understand your requirement.

I tried to show in the screencast that your requirement can be fulfilled with the configuration i used.  I understand you're not seeing the results I showed. Therefore I suggest checking your configuration:

  • simplify your API to reflect what I showed in the screencast. A single API, single target, etc.  
  • make sure you have the ResponseCache policy attached in the way I showed in the screencast. 
  • Use Scope=Application in your ResponseCache policy (not Scope=Exclusive)
  • Eliminate the CacheResource element in your policy
  • Otherwise try to build exactly what I showed in the screencast. 

After you do all this then you should see exactly the results i showed in the screencast - which is what you wanted: the cache is relative to the query param. 

attached please find a starter API bundle that might help you. 

 

 

Top Solution Authors