【问题标题】:Spring and Ehache : CachemangerSpring 和 Ehcache:缓存管理器
【发布时间】:2016-05-05 23:37:51
【问题描述】:

我正在尝试将Eh-Cache 2.8Spring 4.2.3一起使用,我发现我们可以使用spring ehcache manager,但在另一个博客中我发现我们可以直接在spring中直接使用Ehcache manager,也可以使用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 方法中将数据添加到缓存,即不返回任何内容而只想将数据添加到缓存的方法。

【问题讨论】:

  • Cache cache = chachemanager.getCache("cache");这里不应该是EhCache而不是Cache吗?
  • 我是新手,我不太懂
  • 没关系,我们都在这里学习。我不明白你的第二个问题。
  • @user2004685 因为我知道将数据放入缓存的方法是在从数据库或任何数据源获取数据的方法之上添加可缓存或缓存输入,然后返回一些,返回的数据存储在缓存但我有一个方法,它只是从数据库中获取数据并且不返回任何值并且想要存储数据它是如何做到的,代码已经写了订阅数据就足够了

标签: java spring caching ehcache


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

【讨论】:

  • 感谢您的输入,您的意思是说我的 java 配置与我定义的 xml 相同? ...我虽然我正在将实际 ehcache 的缓存管理器配置为 java config 中应用程序的缓存管理器
  • 不是真的,您仍在为此使用 Spring 类型,EhCacheManagerFactoryBeanEhCacheCacheManager
猜你喜欢
  • 1970-01-01
  • 2012-12-12
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
相关资源
最近更新 更多