有人可以通过xml使用Spring CacheManager
帮助创建支持Ehcache的JCacheCacheManager
吗?我有类似的东西。不确定如何为Ehcache3创建javax.cache.CacheManager
。
<bean id="myCacheManager"
class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager" value="..." />
</bean>
谢谢!
答案 0 :(得分:7)
这样做的推荐方法是使用org.springframework.cache.jcache.JCacheManagerFactoryBean
来注入URI
,Properties
和ClassLoader
。然后,此工厂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;
}
}