Cache key query

I am using Response cache for caching on proxy end, one on preflow and other on postflow. After the Response Cache on postflow, I have Json2xml and xml2json polices which run on different conditions.

I should ensure the correct format(xml/json based on headers) when returning responses from cache as well. But as of now that is not happening.

Ex. all the below three request have same uri parameter...

  1. My first request has header Content-Type text/xml which gives me response in xml.
  2. My second request has Content-Type application/json which gives me response in json.
  3. My third request is same as first request but my response is in json(i should get xml as header is text/xml) (this request i think i am getting cache response from my second request)

Any idea what my Response cache should be ? @Anil Sagar

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResponseCache async="false" continueOnError="false" enabled="true" name="Response-Cache">
    <DisplayName>Response-Cache</DisplayName>
    <CacheResource>ProductsCache</CacheResource>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="request.uri" type="string"/>
        <KeyFragment ref="request.header.Content-Type" type="string"/>
        <KeyFragment ref="request.header.Accept" type="string"/>
    </CacheKey>
    <Scope>Exclusive</Scope>
    <SkipCacheLookup>(request.verb != "GET")</SkipCacheLookup>
    <SkipCachePopulation>(response.status.code >= 400)</SkipCachePopulation>
</ResponseCache>
0 7 554
7 REPLIES 7

Not applicable

Hi,

It is depened on your APi proxy implmentation .

I prefer Not to add the Content-Type and Header accept on your cache Key Because the query parameter Might be different for different header and content -Type.SO we should not cache those.

Only preferece is Request URI.

@Binaya Kumar lenka @Anil SagarOnly using Request URI. doesnt help in returning the correct format(xml/json based on he

aders) when returning responses from cache.

Any tips/ideas and quick response is appreciated 🙂

nmunro
New Member

@Barahalikar Siddharth,

There is a setting on the ResponseCache policy that provides the behaviour you require. There's no need to explicitly make the headers part of the key.

A quick review of the apigee docs at the link below should help

Response Cache policy

@Neil Munro, Thanks for answering.

BTW I have actually used below tags, it still did not work. I am confused as of now.

My request headers are Accept and Content-Type

<UseResponseCacheHeaders>true</UseResponseCacheHeaders>

<UseAcceptHeader>true</UseAcceptHeader>

@Barahalikar Siddharth

<UseAcceptHeader>true</UseAcceptHeader>

should give you what you need.

At the risk of stating the obvious, don't forget to clear the cache before testing when you add this. I'm also assuming every request that you expect to use the cache has an Accept header.

@Neil Munro

Yes I am clearing the cache before testing and have only used UseAcceptHeader.


1st request has request header - Accept application/json
2nd request has request header - Accept application/xml

1st request doesn't hit Cache.

But 2nd request is hitting the cache and give JSON response.

@Barahalikar Siddharth

It's difficult to know what is happening without a trace file.

Possibilities are that you may be overwriting the headers or you are transforming the xml into json after the response has been retrieved from the cache, but it could be something else.