如何使用HTTP响应状态代码配置API响应缓存?

时间:2017-03-22 02:29:25

标签: wso2 wso2-am

我使用API​​响应缓存测试了APIM性能。 结果与我的期望略有不同。 如下。

第一个API请求通过API网关从API服务获得404响应状态代码。 所以我预计第二个响应将是来自API响应缓存的404响应状态代码。 但是它返回了200个响应状态代码(与第一个不同)具有相同的响应主体。

我从WSO2文件中读到了下面的一些注释。

  

缓存中介不会在缓存表中缓存HTTP响应的响应状态代码。相反,它会在缓存命中时返回“200 OK”状态代码,这是默认的请求成功状态响应。如果要在请求获得缓存命中时返回不同的状态代码,则可以更新onCacheHit序列中的响应状态代码。

我理解这个音符是什么意思,但我不知道该怎么做。 我的区域不是使用缓存中介,尤其是中介语法。

我希望API响应缓存能够使用HTTP响应状态代码。 有人可以为我指导吗?

1 个答案:

答案 0 :(得分:1)

我找到了解决这个问题的方法 这可能是错的,但它按照我的意图行事 (当HTTP响应代码为200时,API响应缓存可用,以及GET方法。)

我编辑了velopcity_templete.xml文件,如下所示。

<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>

如果有人有更好更标准的方式,对我来说会非常有帮助。

相关问题