cachingspring-bootehcachejcachejsr107

Is it possible to have common xml configuration for all Cache provider vendors for jsr107


We need to have common XML configuration parameters(like timetolive) for Jcache configuration.
We are using EhCache for Development and might be using some other Jsr107 compliant cache provider, like Infinispan, in other environment.

is it possible to have single configuration file being used by both Caching provider, and we need to change only some parameters, if required, for different environment?

Is it ok to define these properties in properties file and use them to initialize Cache managers based on Profile?

I went through jsr107 documentation but didn't find common xml caching attributes.

Technology : Spring boot 1.2.3, Java 7


Solution

  • It really depends on what you need to use. JCache exposes a Configuration and MutableConfiguration classes that you can use to configure some settings.

    Spring Boot 1.3 (about to be released) has a complete JCache integration; when you add a JSR-107 provider in your project, Spring Boot will automatically create a CacheManager for you. If you define a bean of type JCacheManagerCustomizer, it will be invoked to customize the cache manager before the application starts servicing requests.

    For instance, this is a very basic configuration that changes the expiration policy:

    @SpringBootApplication
    @EnableCaching
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Bean
        public JCacheManagerCustomizer cacheManagerCustomizer() {
            return cm -> {
                MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>()
                    .setExpiryPolicyFactory(CreatedExpiryPolicy
                        .factoryOf(Duration.ONE_HOUR));
                cm.createCache("foo", configuration);
            };
        }
    
    }