【问题标题】:Mule Caching Strategy using Redis使用 Redis 的 Mule 缓存策略
【发布时间】:2013-10-13 20:22:11
【问题描述】:

我正在寻找一种跨两台服务器共享缓存的方法,我正在研究将 Redis 用作对象存储缓存策略,但在读取存储值时遇到了问题。

当缓存命中为未命中值时,它成功存储了一个值,但在检索该值时抛出了错误。

所需的对象/属性“muleContext”为空

据推测,object-store-caching-strategy 可能需要一个实现 MuleContextAware 接口的对象存储。

有谁知道这是否正确或如何解决此问题?

这是示例流程

    <mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    xmlns:http="http://www.mulesoft.org/schema/mule/http" 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.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
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/redis http://www.mulesoft.org/schema/mule/redis/3.4/mule-redis.xsd">


    <redis:config name="Redis" doc:name="Redis" defaultPartitionName="test" />
    <ee:object-store-caching-strategy name="Redis_Caching_Strategy" doc:name="Caching Strategy">
        <spring-object-store ref="Redis" />
    </ee:object-store-caching-strategy>

    <flow name="htmlCacheRedisFlow" doc:name="htmlCacheRedisFlow">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8084" path="cacheRedis" doc:name="HTTP"/>
        <expression-transformer expression="#[payload.substring(payload.lastIndexOf('/') + 1)]" doc:name="Expression"/>
        <ee:cache doc:name="Cache" cachingStrategy-ref="Redis_Caching_Strategy" >
            <logger message="getting item from db for key #[payload]" level="INFO" doc:name="Logger"/>
            <expression-transformer expression="#[payload + 'asd']" doc:name="Expression"/>
        </ee:cache>
    </flow> 
</mule>

【问题讨论】:

  • 请记住,由于您使用的是企业版,因此​​您可以联系 MuleSoft 的专业支持寻求帮助。此功能不是社区版的一部分,因此 StackOverflow 上的社区可能无法为您提供帮助……但这是一个很好的问题 :)
  • 好的,谢谢,我没有意识到我仍然将工作室中的运行时设置为 EE。现在我改变了这一点,我看到缓存不再可用,所以这个问题不再适用于我。

标签: mule mule-studio


【解决方案1】:

正如 David 已经指出的,在问题 cmets 中,EE 缓存范围在社区版中不可用。但是,在社区版中有一些方法可以实现缓存。

博文Enterprise caching with Mule ESB Community Edition 展示了如何通过添加自定义拦截器来做到这一点。博客文章使用 ehcache,但您可以修改此示例以使用 Redis。

简而言之就是:

<custom-interceptor doc:name="PayloadCache"   
     class="se.redpill.mulecomponents.cache.PayloadCache">  
   <spring:property name="cache" ref="MyCache"/>  
</custom-interceptor>

和 PayloadCache.java

package se.redpill.mulecomponents.cache;  
import net.sf.ehcache.Ehcache;  
import net.sf.ehcache.Element;  
import org.mule.DefaultMuleEvent;  
import org.mule.DefaultMuleMessage;  
import org.mule.api.MuleEvent;  
import org.mule.api.MuleException;  
import org.mule.api.MuleMessage;  
import org.mule.api.interceptor.Interceptor;  
import org.mule.api.processor.MessageProcessor;  
/**  
 * A mule interceptor acting as a ehCache component.  
 * Based on the Cache interceptor blueprint from Mule In Action by David Dossot and John D'Emic,  
 *   
 */  
public class PayloadCache implements Interceptor   
{       
       private MessageProcessor next;  
       private Ehcache cache;  
       public void setListener(MessageProcessor listener)  
       {  
         next = listener;  
       }  
       public void setCache(final Ehcache cache)  
       {  
         this.cache = cache;  
       }  
       public MuleEvent process(MuleEvent event) throws MuleException  
       {  
         final MuleMessage currentMessage = event.getMessage();  
         final Object key = currentMessage.getPayload();  
         final Element cachedElement = cache.get(key);  
         if (cachedElement != null)  
         {  
           return new DefaultMuleEvent(new DefaultMuleMessage(cachedElement.getObjectValue(),  
             currentMessage, event.getMuleContext()), event);  
         }  
         final MuleEvent result = next.process(event);  
         cache.put(new Element(key, result.getMessage().getPayload()));  
         return result;  
       }  
}  

【讨论】:

  • 如何修改这个例子来使用Mongodb?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 2022-08-08
  • 1970-01-01
  • 2015-09-04
相关资源
最近更新 更多