dav*_*688 3 spring caching ehcache jhipster
我在实现 secong 缓存管理器时遇到了问题。目前我正在使用 EhCache,它工作正常。另外我想实现Java Simple Cache。
我的 CacheConfiguration 看起来像这样:
缓存配置文件
@Configuration
@EnableCaching
@AutoConfigureAfter(value = { MetricsConfiguration.class })
@AutoConfigureBefore(value = { WebConfigurer.class, DatabaseConfiguration.class })
public class CacheConfiguration {
private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
JHipsterProperties.Cache.Ehcache ehcache =
jHipsterProperties.getCache().getEhcache();
jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(ehcache.getTimeToLiveSeconds(), TimeUnit.SECONDS)))
.build());
}
/**
* EhCache configuration
*
* @return
*/
@Bean
@Primary
public JCacheManagerCustomizer cacheManagerCustomizer() {
return cm -> {
cm.createCache(com.david.coinlender.domain.News.class.getName(), jcacheConfiguration);
// ...More caches
}
/**
* Java Simple Cache configuration
* @return
*/
@Bean
@Qualifier("simpleCacheManager")
public CacheManager simpleCacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
simpleCacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("bitfinexAuthCache")));
return simpleCacheManager;
}
Run Code Online (Sandbox Code Playgroud)
}
使用简单缓存我想缓存对象。IE:
@Cacheable(cacheManager = "simpleCacheManager", cacheNames = "bitfinexAuthCache", key = "#apiKey.apiKey")
private Exchange createBitfinexAuthenticatedExchange(ApiKeys apiKey) {
ExchangeSpecification exSpec = new BitfinexExchange().getDefaultExchangeSpecification();
exSpec.setApiKey(apiKey.getApiKey());
exSpec.setSecretKey(apiKey.getSecret());
Exchange bfx = ExchangeFactory.INSTANCE.createExchange(BitfinexExchange.class.getName());
bfx.applySpecification(exSpec);
return bfx;
}
Run Code Online (Sandbox Code Playgroud)
但是,在服务器启动时 liquibase 给了我一个错误说明:
Caused by: java.lang.IllegalStateException: All Hibernate caches should be created upfront. Please update CacheConfiguration.java to add com.david.coinlender.domain.News
at io.github.jhipster.config.jcache.NoDefaultJCacheRegionFactory.createCache(NoDefaultJCacheRegionFactory.java:37)
at org.hibernate.cache.jcache.JCacheRegionFactory.getOrCreateCache(JCacheRegionFactory.java:190)
at org.hibernate.cache.jcache.JCacheRegionFactory.buildEntityRegion(JCacheRegionFactory.java:113)
at org.hibernate.cache.spi.RegionFactory.buildEntityRegion(RegionFactory.java:132)
at org.hibernate.internal.CacheImpl.determineEntityRegionAccessStrategy(CacheImpl.java:439)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:120)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:297)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:445)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
... 25 common frames omitted
Run Code Online (Sandbox Code Playgroud)
我正在为我的应用程序使用 Jhipster 框架。我在谷歌上搜索了这个问题几个小时,但还没有找到解决方案。
这是由于配置错误导致的错误吗?有人可以指出我正确的方向吗?
在 JHipster 中(我做了代码),实际上有两层缓存。您有 Spring Cache 和 Hibernate Second Level Cache。两者都使用相同的实际 Ehcache CacheManager
。
在您的情况下,您已经用 Spring 的简单缓存替换了 Ehcache。但是由于NoDefaultJCacheRegionFactory
仍然是在Hibernate上配置的,所以Hibernate使用的仍然是Ehcache。但不再使用定制器。所以它失败了。
你想要一个简单的 Spring 缓存和 Ehcache 用于 Hibernate。这意味着实际上不需要在 Spring 中为 Ehcache 注册 bean。
最简单的方法是执行以下操作。
首先,在DatabaseConfiguration
. 因此,当休眠 JCache 工厂检索它时,它将被正确配置。
public DatabaseConfiguration(Environment env, JHipsterProperties jHipsterProperties) {
this.env = env;
JHipsterProperties.Cache.Ehcache ehcache =
jHipsterProperties.getCache().getEhcache();
CachingProvider provider = Caching.getCachingProvider();
javax.cache.CacheManager cacheManager = provider.getCacheManager();
javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(ehcache.getTimeToLiveSeconds(), TimeUnit.SECONDS)))
.build());
cacheManager.createCache(com.mycompany.myapp.domain.User.class.getName(), jcacheConfiguration);
cacheManager.createCache(com.mycompany.myapp.domain.Authority.class.getName(), jcacheConfiguration);
cacheManager.createCache(com.mycompany.myapp.domain.User.class.getName() + ".authorities", jcacheConfiguration);
}
Run Code Online (Sandbox Code Playgroud)
然后,配置Spring Cache。
public class CacheConfiguration {
public CacheConfiguration() {
}
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
Collection<Cache> caches = Arrays.asList(
new ConcurrentMapCache("mycache")
// ...
);
cacheManager.setCaches(caches);
return cacheManager;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6343 次 |
最近记录: |