【发布时间】:2016-05-05 23:37:51
【问题描述】:
我正在尝试将Eh-Cache 2.8与Spring 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