2
votes

I tested APIM performance with API Response cache. The result was a little different to my expectation. like below.

The first API reqeust got the 404 response status code from API Service through API Gateway. So I expected the second response would be 404 response status code from API Response cache. However it returned 200 response status code (different to the first) with same response body.

I read some note like below from WSO2 document.

Cache mediator does not cache the response status code of the HTTP response in the cache table. Instead, it returns the "200 OK" status code on a cache hit, which is the default request success status response. If you want to return a different status code when the request gets a cache hit, you can update the response status code in the onCacheHit sequence.

I understood what means the note, but I have no idea how to do that. It's not my area working with cache mediation, especially mediation syntax.

I want API Response cache to work with HTTP Response status code. Can somebody guide for me?

1

1 Answers

1
votes

I found the my own way to solve this problem.
this might be wrong, but it works as I intended.
(API Response cache works, when HTTP Response code is 200, and GET method.)

I edited velopcity_templete.xml file, like below.

<inSequence>
    ...
    ## check and set response caching
    #if($responseCacheEnabled)
    #if($resource.getMethodsAsString() == 'GET')
    <cache scope="per-host" collector="false" hashGenerator="org.wso2.caching.digest.REQUESTHASHGenerator" timeout="$!responseCacheTimeOut">
        <implementation type="memory" maxSize="500"/>
    </cache>
    #end
    ...
</inSequence>

<outSequence>
    <class name="org.wso2.carbon.apimgt.usage.publisher.APIMgtResponseHandler"/>
    ## check and set response caching
    #if($responseCacheEnabled)
    #if($resource.getMethodsAsString() == 'GET')
    <filter regex="200" source="$axis2:HTTP_SC">
            <then>
                <cache scope="per-host" collector="true"/>
            </then>
        </filter>
    #end
    #end
    <send/>
</outSequence>

If anyone has better and more standard way, It will be very helpful for me.