【问题标题】:How to control key generation in Mule Cache如何控制 Mule Cache 中的密钥生成
【发布时间】:2015-03-06 00:31:32
【问题描述】:

我的要求是在我的 Mule Flow 中对用户(使用外部 ws)进行身份验证,并在缓存中使用身份验证结果。但是,如果用户凭据发生更改,则缓存应自动失效,并且必须使用外部 ws 完成用户身份验证。基本上缓存键应该基于用户凭据。这可能吗 ?

这是我的 mule 流程,我看到 Mule 在第一次请求之后缓存结果,无论后续请求中的有效负载是否更改(这是发送凭据的地方),mule 总是从缓存中返回结果。因此,当第一个请求的凭据不正确时,用户身份验证失败并且 mule 缓存响应。从这一点开始,无论在后续请求中是否发送正确的凭据,它始终引用缓存并返回用户身份验证失败。我如何实现我想要实现的目标?

这是我的骡子流程:

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP-Inbound-Endpoint" host="0.0.0.0" port="8888" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" host="10.10.10.10" port="8080" doc:name="HTTP Request Configuration"/>
    <ee:object-store-caching-strategy name="Auth-Cache-Strategy" doc:name="Caching Strategy">
        <in-memory-store name="UserAuthCache" maxEntries="100" entryTTL="3600" expirationInterval="3600"/>
    </ee:object-store-caching-strategy>
    <flow name="cacheauthenticationFlow">
        <http:listener config-ref="HTTP-Inbound-Endpoint" path="*" doc:name="HTTP"/>
        <object-to-string-transformer doc:name="Object to String"/>
        <ee:cache cachingStrategy-ref="Auth-Cache-Strategy" doc:name="Cache">
            <logger message="Incoming: #[message.payload]" level="INFO" doc:name="Logger"/>
            <scripting:transformer doc:name="Python">
                <scripting:script engine="jython"><![CDATA[import base64
authorization = message.getInboundProperty("authorization")
#print "Authorization is: \"" + authorization + "\""
authstring = authorization.split()
#print authstring
credentials = authstring[-1]
#print "Credentials => " + credentials

decodedAuth = credentials.decode('base64')
#print decodedAuth
if (decodedAuth.find("@") > 0):
    (id, password) = decodedAuth.split(":")
    (username, project) = id.split("@")
    print username + ":" + password + ", Project: " + project
else:
    (username, password) = decodedAuth.split(":")
    print username + ":" + password

message.payload = { "username" : username + "@" + project , "password" : password }
result = message]]></scripting:script>
            </scripting:transformer>
            <json:object-to-json-transformer doc:name="Object to JSON"/>
            <logger message="Incoming payload: #[message.payload]" level="INFO" doc:name="Logger"/>
            <http:request config-ref="HTTP_Request_Configuration" path="/wservices/authenticate/user" method="POST" doc:name="HTTP-Dev-Box-Authentication"/>
            <object-to-string-transformer doc:name="Object to String"/>
        </ee:cache>
        <logger message="Response From Cache: #[message.payload]" level="INFO" doc:name="Logger"/>
        <set-payload value="#[message.payload.'status']" doc:name="Response"/>
    </flow>
</mule>

【问题讨论】:

    标签: caching mule


    【解决方案1】:

    由于缓存范围的默认密钥生成策略是基于消息负载的,因此在您的特定情况下,您可以选择简单地将 scripting:transformer 移动到 ee:cache 范围之前执行。

    对于更通用的解决方案,如果您不想覆盖您的请求负载,您可以在 ee:cache 元素上定义 keyGenerationExpressionkeyGenerator-refattribute 来控制我生成缓存密钥的方式。

    例如,使用完整的 HTTP 授权标头作为您可以使用的密钥:

     <ee:cache cachingStrategy-ref="Auth-Cache-Strategy" doc:name="Cache"
          keyGenerationExpression="#[message.inboundProperties.authorization]">
         <!-- Your flow here --> 
     </ee:cache>
    

    请参阅cache scope documentation 了解更多信息。

    如果您想使用这种方法,您可以将部分 jython 代码移出缓存范围,对其进行更改,以便将用户和密码设置为消息上的单独流变量,然后在 @987654328 中使用它们@ 然后在一个简单的set-payload 转换器中再次在缓存范围内。

    【讨论】:

    • 感谢本都。我已经使用了 keyGenerationExpression,它现在可以工作了。但是,我对缓存到期有疑问。我已经配置了一个内存存储。无论我在 TTL 中配置什么,缓存总是每 3-4 个请求命中一次原始 ws。如果我没有设置 TTL 或到期间隔,那么除非我的密钥发生变化,否则它永远不会达到原始 ws。将缓存到期配置为 5 分钟的正确方法是什么。 5分钟后,我的缓存一定要失效,后面的请求一定要引用原来的ws,缓存接下来5分钟的结果,以此类推。
    【解决方案2】:

    您可以定义一个全局配置“Caching_Strategy”

    <ee:object-store-caching-strategy name="Caching_Strategy" keyGenerationExpression="#[flowVars.userID]" doc:name="Caching Strategy"/>
    

    并参考缓存流中的全局配置:

    <ee:cache doc:name="Cache" cachingStrategy-ref="Caching_Strategy">
    <!-- flow -->
    </ee:cache>
    

    您可以通过流变量#[flowVars.userID]

    控制您的keyGenerationExpression

    有关示例的完整详细信息,请参阅 http://www.tutorialsatoz.com/caching-in-mule-cache-scope/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      相关资源
      最近更新 更多