春季:多个缓存管理器 [英] Spring: Multiple Cache Managers

查看:129
本文介绍了春季:多个缓存管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现第二缓存管理器时遇到问题。目前,我正在使用EhCache,效果很好。另外,我想实现Java简单缓存。

I have a problem implementing a secong cache managers. At the moment I'm using EhCache, which is working fine. Additionally I would like to implement Java Simple Cache.

我的CacheConfiguration看起来像这样:

My CacheConfiguration looks like this:

CacheConfiguration.java

CacheConfiguration.java

@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;

}

}

使用简单缓存,我想缓存对象。即:

With Simple Cache I want to cache objects. I.e.:

@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;
}

但是,在服务器启动时,liquibase提示我错误:

However, on Server startup liquibase gives me an error stating:

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

我正在使用Jhipster框架进行应用。我用几个小时搜索了这个问题,但还没有找到解决方法。

I'm using the Jhipster framework for my appication. I googled this issue for hours now and haven't found a solution yet.

此错误是由于配置错误引起的吗?有人可以指出正确的方向吗?

Is this error due to wrong configuration? Can someone please point me in the right direction?

推荐答案

在JHipster中(我做了代码),实际上有两层缓存。您具有Spring Cache和Hibernate Second Level Cache。两者都使用相同的实际Ehcache CacheManager

In JHipster (I did the code), there is in fact two layers of cache. You have Spring Cache and Hibernate Second Level Cache. Both are using the same actual Ehcache CacheManager.

在您的情况下,您已将Ehcache替换为简单的缓存春天。但是由于 NoDefaultJCacheRegionFactory 仍在Hibernate上配置,因此仍将Ehcache用于Hibernate。但是不再使用定制程序。因此失败了。

In your case, you've replaced Ehcache with a simple cache for Spring. But since the NoDefaultJCacheRegionFactory is still configured on Hibernate, it is still Ehcache that is used for Hibernate. But the customizer isn't used anymore. So it fails.

您想为Spring提供一个简单的缓存,为Hibernate提供一个Ehcache。这意味着实际上在春季不需要为Ehcache注册Bean。

You would like to have a simple cache for Spring and Ehcache for Hibernate. This means that in fact to don't need to have bean registered for Ehcache in Spring.

然后,最简单的方法是执行以下操作。

The easiest is then to do the following.

首先,在 DatabaseConfiguration 中配置Ehcache。因此,当休眠JCache工厂检索它时,将对其进行正确配置。

First, configure Ehcache in DatabaseConfiguration. So when the hibernate JCache factory will retrieve it, it will be correctly configured.

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);
}

然后,配置Spring Cache。

Then, configure 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;
    }
}

这篇关于春季:多个缓存管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆