在Spring4上使用ehcache3

时间:2017-02-17 10:03:21

标签: spring ehcache

有人可以通过xml使用Spring CacheManager帮助创建支持Ehcache的JCacheCacheManager吗?我有类似的东西。不确定如何为Ehcache3创建javax.cache.CacheManager

<bean id="myCacheManager"
    class="org.springframework.cache.jcache.JCacheCacheManager">
    <property name="cacheManager" value="..." />
</bean>

谢谢!

3 个答案:

答案 0 :(得分:7)

这样做的推荐方法是使用org.springframework.cache.jcache.JCacheManagerFactoryBean来注入URIPropertiesClassLoader。然后,此工厂bean将使用标准JCache Caching类来创建javax.cache.CacheManager

对于Ehcache,URI用于指向ehcache.xml,然后将CacheManager配置<bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean"> <property name="cacheManagerUri" value="file://path/to/ehcache.xml"/> </bean> <bean id="myCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager"> <property name="cacheManager" ref="jCacheManager" /> </bean>

扩展您的示例配置:

import UIKit

let date =  Date(timeIntervalSince1970: 0)
var formatter = DateFormatter()
formatter.dateFormat = "HH:mm"

print(TimeZone.current.abbreviation()!) // GMT

formatter.timeZone = TimeZone.current
print(formatter.string(from: date)) // 01:00

formatter.timeZone = TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
print(formatter.string(from: date)) // 00:00

formatter.timeZone = TimeZone(identifier: "Europe/London")
print(formatter.string(from: date)) // 01:00

formatter.timeZone = TimeZone(identifier: "America/Los_Angeles")
print(formatter.string(from: date)) // 16:00

有关Ehcache 3 / JCache集成的更多信息,请参阅the documentation

答案 1 :(得分:0)

@Configuration
@EnableCaching
public class CacheConfig {

  @Bean
  public JCacheCacheManager jCacheCacheManager() throws IOException {
    return new JCacheCacheManager(cacheManager());
  }

  @Bean(destroyMethod = "close")
  public javax.cache.CacheManager cacheManager() throws IOException {
    XmlConfiguration xmlConfig = new XmlConfiguration(new ClassPathResource("ehcache.xml").getURL());
    EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
    return provider.getCacheManager(provider.getDefaultURI(), xmlConfig);

  }

}

答案 2 :(得分:0)

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public JCacheCacheManager jCacheCacheManager(JCacheManagerFactoryBean jCacheManagerFactoryBean){
        JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
        jCacheCacheManager.setCacheManager(jCacheManagerFactoryBean.getObject());
        return jCacheCacheManager;
    }

    @Bean
    public JCacheManagerFactoryBean jCacheManagerFactoryBean() throws URISyntaxException {
        JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
        jCacheManagerFactoryBean.setCacheManagerUri(getClass().getResource("/ehcache.xml").toURI());
        return jCacheManagerFactoryBean;
    }
}