Abhi V
Abhi V

Reputation: 41

Unable to use EhCacheCacheManager in SpringBoot 3

I am trying to migrate my Spring Boot app from v2 to v3. I have the below caching config working in a Spring Boot app version 2.7.7 and ehcache from net.sf.ehcache version 2.7.4.

I am unable to migrate this same config to Spring Boot 3. My understanding is this is a very old version of EhCache but I was wondering if there's a way or workaround to make this config work in Spring Boot 3. I want to avoid additional changes at this point if I can make the current config backward compatible.

If not, what's the best way to update this class without usage of xml configurations.

import net.sf.ehcache.config.CacheConfiguration;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
// this below import doesn't work in Spring Boot 3 (version 3.1.4)
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@EnableCaching
public class AppConfig implements CachingConfigurer {

    private static final List<String> CACHE_LIST =List.of("entity1", "entity2", "entity3");

    @Bean(destroyMethod = "shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        final net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
        for (final String cacheComponent : CACHE_LIST) {
            final CacheConfiguration cacheConfiguration = new CacheConfiguration();
            cacheConfiguration.setName(cacheComponent);
            cacheConfiguration.setMemoryStoreEvictionPolicy("LFU");
            cacheConfiguration.setMaxEntriesLocalHeap(10000);
            cacheConfiguration.setOverflowToOffHeap(false);
            cacheConfiguration.setEternal(false);
            configuration.addCache(cacheConfiguration);
        }
        return net.sf.ehcache.CacheManager.newInstance(configuration);
    }

    @Bean
    @Override
    // cannot resolve symbol 'EhCacheCacheManager' with Spring Boot 3
    public EhCacheCacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManager());
    }

}

Upvotes: 1

Views: 1621

Answers (0)

Related Questions