java - Spring和Ehache : Cachemanger

标签 java spring caching ehcache

我尝试将Eh-Cache 2.8Spring 4.2.3一起使用,我发现我们可以使用spring ehcache管理器,但在另一篇博客中我发现我们可以直接在spring中直接使用Ehcache管理器并使用Ehache注解。我很困惑哪种方法更好、更正确。 以下是代码配置:

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:cache='http://www.springframework.org/schema/cache' 
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee.xsd
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/jms  
    http://www.springframework.org/schema/jms/spring-jms.xsd
">

    <context:annotation-config />
    <cache:annotation-driven />
    <tx:annotation-driven proxy-target-class="true" />
    <tx:jta-transaction-manager />

    <bean id='ehcache'
class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean'
        p:configLocation='classpath:ehcache.xml' p:shared='true' />

    <bean id='cacheManager' class='org.springframework.cache.ehcache.EhCacheCacheManager'
        p:cacheManager-ref='ehcache' />

</beans>

我将数据添加到缓存的代码

SubscribeData.java 该类从MQ获取数据并将其添加到缓存

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

public class SubscribeData implements MessageListener {

    @Autowired
    CacheManager chachemanager;

    public void onMessage(Message m) {
        TextMessage message = (TextMessage) m;
        try {
            Cache cache = chachemanager.getCache("cache");
            cache.put("key", message.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

SubscribeData.class 中,我从 xml 获取数据并将其添加到缓存中,这是正确的方法。我知道我在这里没有使用缓存注释,但我还使用其他方法。

以下是我的疑问

1)如上所述,我应该直接使用 spring ehCache 还是 ehCache,示例如下 ehCache 作为缓存管理器而不是 spring ehCache 管理器

@Configuration
@EnableCaching(proxyTargetClass = true)
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        net.sf.ehcache.CacheManager ehcacheCacheManager = ehCacheManagerFactoryBean().getObject();
        return new EhCacheCacheManager(ehcacheCacheManager);
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean cacheMgrFB = new EhCacheManagerFactoryBean();

        cacheMgrFB.setShared(true);
        cacheMgrFB.setCacheManagerName("cacheManager");
        cacheMgrFB.setConfigLocation(ehCacheConfigResource);
        return cacheMgrFB;
    }
}

2) 如何在 void 方法中添加数据到缓存,即不返回任何内容而只想将数据添加到缓存的方法。

最佳答案

  1. 对于你的第一个问题,你在第二个示例中并没有真正使用 Ehcache API,只是编写了与上面的 Spring XML 等效的代码。因此,在这种情况下,除非您有充分的理由为此编写代码,否则我会坚持使用 XML,因为如果有一天需要的话,它将使更新配置变得更容易。
  2. 实际上,您自己在 SubscribeData 示例中提供了答案。您不能使用 void 方法,因为执行包装的代理无法访问需要发布的数据。

关于java - Spring和Ehache : Cachemanger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35064130/

相关文章:

java - 设计AppServer面试讨论

java - 如何使用 Hibernate 和 Spring Data JPA 正确注释两个实体之间的关系?

c# - 使用单例进行缓存

java - 在 mac os mavericks 上将 mySQL 连接到 java netbeans 时出错

java - 结果集开始之前

java - Postgres 的搜索路径不适用于 Spring/DBCP 数据源

java - Spring MVC 网站管理/配置页面的推荐数据模型和数据源?

java - Hibernate - 集群缓存和更新类版本

Java 树节点着色

java - Maven 抛出空指针异常