如何缓存数据库查询结果并避免缓存处理骡子消息而不考虑传入请求?

时间:2018-03-21 14:30:00

标签: caching mule mule-component mule-esb

我正在缓存来自数据库的第三方API网址,后续传入的邮件必须使用此网址才能访问第三方系统。

目前每当我对传入请求进行任何更改时,都会再次从数据库加载URL,并且何时一次又一次地发送相同的请求,然后从缓存中获取URL。

但是我想首次加载URL,然后应该从缓存中获取,而不管传入请求中的内容是什么。

请让我知道怎么做?

MULE XML:

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="lcdre342.cdr-p01.chp.bankofamerica.com" port="20108" basePath="/ngen/AdministerAccountRelationshipManagement/V001/administer-accounts" doc:name="HTTP Request Configuration"/>
<http:request-config name="HTTP_Outgoing_Request" host="#[sessionVars.api_url]" port="${mule.env.port}" doc:name="HTTP Request Configuration"/>
<context:property-placeholder location="${mule.env}.properties"/>
<db:generic-config name="Generic_Database_Configuration" url="jdbc:db2://db2dvipa9sd92t.bankofamerica.com:446/D92T:user=${mule.env.dbuserName};password=${mule.env.dbPassword};" driverClassName="com.ibm.db2.jcc.DB2Driver" doc:name="Generic Database Configuration"/>
<ee:object-store-caching-strategy name="API_Url_cache" doc:name="Caching Strategy">
    <managed-store storeName="API URL Managed Store" persistent="true" maxEntries="1" entryTTL="600000" expirationInterval="6000"/>
</ee:object-store-caching-strategy>
<flow name="ringfencedemoFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/administer-accounts" allowedMethods="POST" doc:name="HTTP">
        <http:response-builder statusCode="#[message.inboundProperties.'http.status']" reasonPhrase="#[message.inboundProperties.'http.reason']">
            <http:header headerName="x-boa-site-affinity" value="#[message.inboundProperties.'x-boa-site-affinity']"/>
            <http:header headerName="x-boa-site-affinity-wcc" value="#[message.inboundProperties.'x-boa-site-affinity-wcc']"/>
            <http:header headerName="x-boa-trace-id" value="#[message.inboundProperties.'x-boa-trace-id']"/>
        </http:response-builder>
    </http:listener>
    <object-to-string-transformer doc:name="Object to String"/>
    <logger message="#[payload]" level="INFO" doc:name="Input Request Logger"/>
    <set-variable variableName="inputMsg" value="#[payload]" doc:name="Put input request into variable"/>
    <ee:cache doc:name="Cache" cachingStrategy-ref="API_Url_cache" filterExpression="#[payload.isEmpty() == false]">
        <db:select config-ref="Generic_Database_Configuration" doc:name="Get API url from DB">
            <db:parameterized-query><![CDATA[select data_value from ${mule.env.schemavalue}.XWCCSYSPARM where GROUPREFID='WCCD' and CATEGORY='MULE' and KEY_ID='WCC_API_URL' and SEQ_NUM=0]]></db:parameterized-query>
        </db:select>
    </ee:cache>
    <set-session-variable variableName="api_url" value="#[payload.get(0).DATA_VALUE]" encoding="UTF-8" mimeType="text/plain" doc:name="Set Api url in session variable"/>
    <set-payload value="#[flowVars.inputMsg]" encoding="UTF-8" mimeType="application/xml" doc:name="Set back input request in payload"/>
    <http:request config-ref="HTTP_Outgoing_Request" path="${mule.env.path}" method="POST" doc:name="Call to WCC System">
        <http:request-builder>
            <http:header headerName="x-boa-user-id" value="#[message.inboundProperties.'x-boa-user-id']"/>
            <http:header headerName="X-BOA-Trace-ID" value="#[message.inboundProperties.'X-BOA-Trace-ID']"/>
            <http:header headerName="X-BOA-RDS-Auth-ChannelId" value="#[message.inboundProperties.'X-BOA-RDS-Auth-ChannelId']"/>
            <http:header headerName="X-BOA-RDS-Auth-AppId" value="#[message.inboundProperties.'X-BOA-RDS-Auth-AppId']"/>
            <http:header headerName="X-BOA-Security-Token" value="#[message.inboundProperties.'X-BOA-Security-Token']"/>
            <http:header headerName="X-BOA-User-ID-Type" value="#[message.inboundProperties.'X-BOA-User-ID-Type']"/>
            <http:header headerName="X-BOA-Originator-Component" value="#[message.inboundProperties.'X-BOA-Originator-Component']"/>
        </http:request-builder>
        <http:success-status-code-validator values="200,400,207"/>
    </http:request>
    <object-to-string-transformer doc:name="Object to String"/>
   <!--  <component doc:name="PrettyPrintXML" class="org.boa.format.PrettyPrintXML"/> -->
    <logger message="#[payload]" level="INFO" doc:name="Response logger"/>
</flow>

1 个答案:

答案 0 :(得分:0)

您需要在缓存策略中设置 keyGenerationExpression 属性,如下所示:

<ee:object-store-caching-strategy name="API_Url_cache" doc:name="Caching Strategy"
keyGenerationExpression="#[flowVars.someConstantVariable]">
   <managed-store storeName="API URL Managed Store" persistent="true" maxEntries="1" entryTTL="600000" expirationInterval="6000"/>
</ee:object-store-caching-strategy>

高速缓存条目基于生成的密钥存储在高速缓存中。下次执行缓存作用域时,新生成的密钥与存储的密钥匹配,以确定它是CACHE HIT还是CACHE MISS。

在您的情况下,Mule的默认密钥生成器生成的密钥可能对每个消息请求都是唯一的。因此,每次都执行缓存范围。

来自https://docs.mulesoft.com/mule-user-guide/v/3.8/cache-scope - enter image description here

常量密钥生成的几个例子:

// Generates same key whenever payload is not empty
keyGenerationExpression="#[!payload.isEmpty()]" 

// Generates same key for a calendar month
keyGenerationExpression="#[server.dateTime.month]" 

HTH。