2

ehcache を統合して L2cache を休止状態にしたい 100 を超えるドメイン モデルを持つアプリケーションがあります。私のアプリケーションは、サービス メソッドの一部をキャッシュするために ehcache を使用しました。私のCacheConfigurationはこのようなものです

 package org.roshan.framework.config;

import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import org.ehcache.jsr107.Eh107Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;
import java.util.concurrent.TimeUnit;


@Configuration
@EnableCaching
@AutoConfigureAfter(value = {DatabaseConfiguration.class})
public class CacheConfiguration {

    private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class);
    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;


    @PreDestroy
    public void destroy() {
        log.info("Remove Cache Manager metrics");
        log.info("Closing Cache Manager");
    }

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

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        log.debug("Starting Ehcache");

        return cm -> {
            // some cache for using in method of service
            cm.createCache("baseInfoCache", jcacheConfiguration);
            cm.createCache("attachments", jcacheConfiguration);
        };
    }

}

application.yml で、休止状態のキャッシュ設定を次のように変更します

spring:
    devtools:
        restart:
            enabled: true
        livereload:
            enabled: true # we use gulp + BrowserSync for livereload
    application:
        name: roshanframework
    jpa:
        open-in-view: true
        hibernate:
            ddl-auto: none
        properties:
              hibernate.cache.use_second_level_cache: true
              hibernate.cache.use_query_cache: false
              hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory
              hibernate.generate_statistics: false
              hibernate.dialect : org.hibernate.dialect.MySQL5Dialect
              #hibernate.default_schema : ihsl
              hibernate.show_sql : true
              hibernate.current_session_context_class:  org.springframework.orm.hibernate5.SpringSessionContext

しかし、アプリケーションを起動すると、この問題を解決する方法として 2 つのキャッシュマネージャーがあるという例外が発生します。

The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:90)
    at org.hibernate.cache.spi.RegionFactory.start(RegionFactory.java:63)
    at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:71)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:28)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:20)
    at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:58)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
    ... 45 common frames omitted
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
    at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:391)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:269)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:69)
    ... 51 common frames omitted

2 つのキャッシュ マネージャーが存在する理由がわかりません。休止状態とメソッドの両方に 1 つのキャッシュ マネージャーを使用するように構成を変更するにはどうすればよいですか?

4

1 に答える 1