Sador La
Sador La

Reputation: 65

IS there a way to configure ehcache 2 for spring boot 3?

I am migrating some legacy app from sb 2 ( spring boot) to sb 3. App is using ehcache with xml configuration to it. In my cacheConfig class I'm using something like this:

import org.springframework.cache.ehcache.EhCacheCacheManager;


@Configuration
@EnableCaching
class Config {
    @Bean
    CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManager())
    }
 
    @Bean(destroyMethod = 'shutdown')
    net.sf.ehcache.CacheManager ehCacheManager() {
     
 URL url = ("/configFile.xml");
    return net.sf.ehcache.CacheManager.newInstance(url);
    }

however import org.springframework.cache.ehcache.EhCacheCacheManager; is no longer present in spring context support 6+ (spring boot 3).

Is there any way to work around it ? How can I configure my ehcache with new spring boot 3?

Upvotes: 2

Views: 7339

Answers (2)

Prince Agrawal
Prince Agrawal

Reputation: 11

I have mentioned the below steps for migrating to Ehcache 3, it worked for me hope it helps!!

  1. Please update the ehcache.xml to the below XML configuration as this is supported currently in ehcache 3 versions

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.ehcache.org/v3"
        xsi:schemaLocation="http://www.ehcache.org/v3 
        http://www.ehcache.org/schema/ehcache-core-3.10.xsd">
    
    <cache alias="test_cache">
        <expiry>
            <ttl unit="seconds">360000</ttl>
        </expiry>
        <resources>
            <heap unit="entries">300</heap>
        </resources>
    </cache>
    
    <cache alias="test_cache2">
        <expiry>
            <ttl unit="seconds">3600000</ttl>
        </expiry>
        <resources>
            <heap unit="entries">300</heap>
        </resources>
    </cache></config>
    
  2. Create a simple EhcacheConfig class

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class EhCacheConfig {
}
  1. Update your application properties

    spring.cache.jcache.config=classpath:ehcache.xml

** Please note Ehcache 3 has its own cache eviction algorithms in place so we don't have to specify LRU,LFU. ** You can also specify offheap-unit and disk options in the ehcache.xml depending on your usage.

Also if you want to go into details I have written a blog, do please check out it is pretty interesting Migrating Ehcache2 to Ehcache2

Kindly upvote if this helped you!!

Upvotes: 1

sweintritt
sweintritt

Reputation: 564

ehcache 2 is no longer supported as cache provider in Spring Boot 3. You would have to use ehcache 3 with JSR-107 annotations: https://docs.spring.io/spring-boot/docs/3.0.8/reference/html/io.html#io

Upvotes: 1

Related Questions