简体   繁体   English

缓存介体wso2 esb API

[英]Cache Mediator wso2 esb api

I have created an API in wso2 esb to call the rest-service. 我在wso2 esb中创建了一个API来调用rest-service。 I have used cache mediator, it's working fine for a normal GET method. 我已经使用了缓存介体,对于正常的GET方法来说,它工作正常。

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. 我想要的是,我已经使用GET方法获取员工详细信息,我将通过url传递employee_id。

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. 对于给定的employee_id,此API调用是首次访问该服务,然后对于同一个employee_id的下一次调用,它应从缓存本身获取响应,直到给定的超时时间。 if i change the employee_id then it should hit the service but it is getting the response from the cache. 如果我更改了employee_id,那么它应该启动服务,但是它正在从缓存中获取响应。 for different employee_id also. 也适用于不同的employee_id。

My API is, 我的API是

<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, 我的API调用将为

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. 您可以尝试使用哈希生成器的新实现,该实现也使用API​​ URI生成哈希值。

In order to apply that, set the hash generator to org.wso2.carbon.mediator.cache.digest.REQUESTHASHGenerator . 为了应用它,请将哈希值生成器设置为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.) (PS:我仅在WSO2 ESB 4.9.0版本中对此进行了测试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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