LeO*_*LeO 3 java spring ehcache
在我们的项目中,我们使用ehcache2.x,现在我们想迁移到3.x,但是以某种方式我失败了。在2.x中,我们有配置ehcache.xml。我读到的是,使用3.x时也需要更改内容。
但是首先,我不知道如何单独连接缓存。我有
@EnableCaching
@Configuration
public class CacheConf {
@Bean
public CacheManager cacheManager() {
final CacheManagerBuilder<org.ehcache.CacheManager> ret = CacheManagerBuilder.newCacheManagerBuilder();
ret.withCache("properties", getPropCache());
ret.withCache("propertyTypes", getPropCache());
return (CacheManager) ret.build(true);
}
....
Run Code Online (Sandbox Code Playgroud)
但是,我得到的这种配置java.lang.ClassCastException: org.ehcache.core.EhcacheManager cannot be cast to org.springframework.cache.CacheManager意味着我无法ehcache正确设置。
注意:在2.x版本中,我们配置了不同类型的缓存,或多或少是由于有效期-以及其他对象类型存储在其中。
提示使其运行,因为在Spring 5和ehcache3.x中找不到有效的示例?最好是没有xml的配置!
Ehcache CacheManager不是Spring CacheManager。这就是为什么你得到了ClassCastException。
Ehcache 3符合JSR107(JCache)。因此,Spring将使用该连接到它。
您将在这里找到一个示例,并在Spring Boot petclinic中找到一个简单的示例。
如果我以你为榜样,你会做类似的事情
@EnableCaching
@Configuration
public class CacheConf {
@Bean
public CacheManager cacheManager() {
EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
caches.put("properties", getPropCache());
caches.put("propertyTypes", getPropCache());
DefaultConfiguration configuration = new DefaultConfiguration(caches, provider.getDefaultClassLoader());
return new JCacheCacheManager(provider.getCacheManager(provider.getDefaultURI(), configuration));
}
private CacheConfiguration<?, ?> getPropCache() {
// access to the heap() could be done directly cause this returns what is required!
final ResourcePoolsBuilder res = ResourcePoolsBuilder.heap(1000);
// Spring does not allow anything else than Objects...
final CacheConfigurationBuilder<Object, Object> newCacheConfigurationBuilder = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class, res);
return newCacheConfigurationBuilder.build();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1032 次 |
| 最近记录: |