使用Redis的Mule缓存策略 [英] Mule Caching Strategy using Redis

查看:121
本文介绍了使用Redis的Mule缓存策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

所需的对象/属性"muleContext"为空

The required object/property "muleContext" is null

猜测,似乎对象存储高速缓存策略可能需要实现MuleContextAware接口的对象存储.

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?

这是示例流程

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

推荐答案

正如David所指出的那样,在问题注释中,EE缓存范围在社区版中不可用.但是,有一些方法可以在社区版中实现缓存.

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.

博客文章使用Mule ESB社区版进行企业缓存显示了如何通过添加自定义拦截器来做到这一点.博客文章使用ehcache,但您可以修改此示例以使用Redis.

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>

和PayloadCache.java

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

这篇关于使用Redis的Mule缓存策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆