YogeshK
YogeshK

Reputation: 47

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

Upvotes: 0

Views: 1027

Answers (2)

Louis Jacomet
Louis Jacomet

Reputation: 14500

JSR-107 does not specify anything with regards to external configuration - xml, properties, you name it.

As such any externalized configuration solution will have to be provided by your code or a framework like [Spring][1].

[1]: See Stéphane Nicoll's answer

Upvotes: 0

Stephane Nicoll
Stephane Nicoll

Reputation: 33151

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

}

Upvotes: 0

Related Questions