0

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

YogeshK
  • 47
  • 11
  • As mentioned by Stéphane Nicoll and Louis, it is not possible to have common config file for all. But you can use auto configuration feature provided in Spring boot 1.3. You can find gitHub example below, contributed by Stéphane Nicoll https://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache – YogeshK Nov 18 '15 at 13:08

2 Answers2

0

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

}
Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • Requirement is, "_As a product-developer, I want to define 2 cache groups: reference-data and interest-rates having different characteristics, e.g. timeToLive. I want to define this only once - to be used by EHCache and JBOSS Infinispan_". We are trying to create plugable Cache configuration microservice which can be used for different JCache managers providers with single xml or properties file changes. – YogeshK Nov 16 '15 at 13:10
  • As there are no common attributes for xml config, will be using providers specific config on class path, spring profile and scope to decide which provider to use at runtime. – YogeshK Nov 17 '15 at 13:04
  • You can use `spring.cache.jcache.config` to point to the actual configuration file if the underlying JSR-107 provider supports it (Ehcache and infinispan do) – Stephane Nicoll Nov 17 '15 at 13:23
  • How do I create instance of CacheManager for Infinispan inside below cacheManager() method which is expected to autocofigure CacheManager once ```spring.cache.infinispan.config=infinispan.xml'``` is defined? ```@Bean @Override public CacheManager cacheManager() { // configure and return CacheManager instance } ``` – YogeshK Nov 18 '15 at 04:20
  • it's a `1.3.0` feature. Can we stop discussing here please? it's not very useful; If you have another question, please create a separate thread. – Stephane Nicoll Nov 18 '15 at 10:10
  • Sorry for the confusion, I had updated the question but forgot delete the comments. I was looking for AutoConfig with 1.3 only for testing purpose, which you have provided here https://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache – YogeshK Nov 18 '15 at 13:02
0

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

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
  • Right, for time being, will be using Spring profile to create required jCachemanager instance at runtime. Thanks for reply :) – YogeshK Nov 17 '15 at 13:10