简体   繁体   中英

Cache Mediator wso2 esb api

I have created an API in wso2 esb to call the rest-service. I have used cache mediator, it's working fine for a normal GET method.

What i want is, I have used a GET Method to get the employee details, I will be passing the employee_id with the url.

This API call is hitting the service for the first time for a given employee_id, then for next call for the same employee_id it should get the response from the cache itself up-to the given Timeout period. if i change the employee_id then it should hit the service but it is getting the response from the cache. for different employee_id also.

My API is,

<api xmlns="http://ws.apache.org/ns/synapse" name="cacheApi" context="/cacheApi">
<resource methods="GET" uri-template="/{id}/">
  <inSequence>
     <log/>
     <cache id="" scope="per-host" collector="false" hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator" timeout="60">
        <implementation type="memory" maxSize="10"/>
     </cache>
     <send>
        <endpoint>
           <http method="GET" uri-template="http://localhost:8080/rest-services/services/employee/{uri.var.id}"/>
        </endpoint>
     </send>
  </inSequence>
  <outSequence>
     <cache scope="per-host" collector="true"/>
     <send/>
  </outSequence>
</resource>
</api>

My API call will be as,

http://localhost:8280/cacheApi/1
http://localhost:8280/cacheApi/2
http://localhost:8280/cacheApi/3
....

Can anyone help me to solve this??

Thanks in advance

您需要实现自己的哈希生成器(实现org.wso2.carbon.mediator.cache.digest.DigestGenerator ),因为org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator哈希仅基于请求的主体/ msgContext.getEnvelope().getBody()并且不适用于您的API。

There is a new implementation of the hash generator that you can try, which uses the API URI as well for generating the hash value.

In order to apply that, set the hash generator to org.wso2.carbon.mediator.cache.digest.REQUESTHASHGenerator .

Now your cache mediator should like this,

<cache id="" scope="per-host" collector="false" hashGenerator="org.wso2.carbon.mediator.cache.digest.REQUESTHASHGenerator" timeout="60">
        <implementation type="memory" maxSize="10"/>
</cache>

(PS: I only tested this for the WSO2 ESB 4.9.0 release.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM