Mule Caching Strategy using Redis

我与影子孤独终老i 提交于 2019-12-19 03:24:09

问题


I'm looking for a way to have a shared cache across two servers and I am investigating using Redis as an object-store-caching-strategy but I am encountering a problem when reading stored values.

It successfully stores a value when the cache hit is a miss value but throws an error when retrieving the value.

The required object/property "muleContext" is null

At a guess it seems like the object-store-caching-strategy might need an object store that implements the MuleContextAware interface.

Does anyone know if this is correct or how to resolve this issue?

Here is the example flow

    <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>

回答1:


As already noted by David, in the question comments, the EE cache scope is not available in the community edition. However there are ways to implement caching in community edition.

The blog post Enterprise caching with Mule ESB Community Edition shows how you can do this by adding a custom interceptor. The blog post uses ehcache but you could modify this example to use Redis instead.

The blog post in short is:

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

and 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;  
       }  
}  


来源:https://stackoverflow.com/questions/19221828/mule-caching-strategy-using-redis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!