4 Replies Latest reply on Jan 13, 2017 9:28 AM by sebastian.laskawiec

    Question about infinispan-cdi.

    seto
      @ConfigureCache("name-cache")
      @NameCache
      @Produces
      public Configuration nameCacheConfig;
      
      @Produces
      @ApplicationScoped
      public EmbeddedCacheManager defaultClusteredCacheManager() {
        System.out.println("new cache manager");
         GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
         ConfigurationBuilder builder = new ConfigurationBuilder();
         builder.clustering().cacheMode(CacheMode.DIST_SYNC);
        return new DefaultCacheManager(global.build(), builder.build());
      }
      

      The nameCacheConfig will use the config from defaultClusteredCacheManager.

        @ConfigureCache("name-cache")
        @NameCache
        @Produces
         public Configuration nameCacheConfig() {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.storeAsBinary().enable();
         return builder.build();
         }
      

      Then it won't use the the config from defaultClusteredCacheManager.

       

      How can I inherit the config from defaultClusteredCacheManager and override the config I need?

      Or do I have to make extract a method to do it?

        • 1. Re: Question about infinispan-cdi.
          sebastian.laskawiec

          When you inject a Cache, the CDI extension tries to find proper configuration (usually a cache configuration and injected Cache objects have the same qualifiers; see this piece of the code). If there is no configuration, a default one is applied.

           

          So if I understood you correctly, you inject multiple Cache objects and you want them to have the same Configuration? If so, you can:

          • Update your producer signature to something like this:
            • @Produces
              public EmbeddedCacheManager defaultClusteredCacheManager(Configuration configuration){
              ...
              return new DefaultCacheManager(..., configuration);
              }
            • Note that Configuration is injected into the producer by CDI
            • With this approach each newly created Cache will have the same configuration unless there is a specific Configuration objects matched by Qualifiers.
          • Disable CDI extension and write your own logic for injecting caches.
          • 2. Re: Question about infinispan-cdi.
            seto

            Not exactly. What I want is a default settings and then with some specific settings to override it.

            Here's a small example.

            @ConfigureCache("name-cache")
            @NameCache
            @Produces
            public Configuration nameCacheConfig() {
               return distributeConfig(defaultConfig()).build();
            }
            
            @ConfigureCache("id-cache")
            @IdCache
            @Produces
            public Configuration idCacheConfig() {
               return distributeConfig(defaultConfig()).build();
            }
            
            @ConfigureCache("local-name-cache")
            @LocalNameCache
            @Produces
            public Configuration localNameCacheConfig()
            {
               return localConfig(defaultConfig()).build();
            }
            
            @ConfigureCache("local-id-cache")
            @LocalIdCache
            @Produces
            public Configuration localIdCacheConfig()
            {
               return localConfig(defaultConfig()).build();
            }
            
            private ConfigurationBuilder distributeConfig(ConfigurationBuilder builder)
            {
              builder.clustering().cacheMode(CacheMode.DIST_SYNC);
              return builder;
            }
            
            private ConfigurationBuilder localConfig(ConfigurationBuilder builder)
            {
              builder.clustering().cacheMode(CacheMode.LOCAL);
              return builder;
            }
            
            private ConfigurationBuilder defaultConfig()
            {
              ConfigurationBuilder builder = new ConfigurationBuilder();
               builder.storeAsBinary().enable();
              return builder;
            }
            
            @Produces
            @ApplicationScoped
            public EmbeddedCacheManager defaultClusteredCacheManager() {
              System.out.println("new cache manager");
               GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
               ConfigurationBuilder builder = distributeConfig(defaultConfig());
              return new DefaultCacheManager(global.build());
            }
            
            public void killCacheManager(@Disposes EmbeddedCacheManager cacheManager) {
              System.out.println("kill cache manager");
               cacheManager.stop();
            }
            
            • 3. Re: Question about infinispan-cdi.
              seto

              I want a base config. And a specific config based on the default one.

              The code above is what I have done to achieve it.

              A multiple @ConfigureCache which I asked for in the issue can resolve the problem of cache sharing the same configuration.

              But I want to have a default config. And the ability to inherit the base config.

              If I want to share the same config. I can just use the code below. But I can't make any changes based on the default config.

              @ConfigureCache("name-cache")  
              @NameCache  
              @Produces  
              public Configuration nameCacheConfig;  
              
              • 4. Re: Question about infinispan-cdi.
                sebastian.laskawiec

                I'm afraid it can not be further optimized. Your approach seems to be the best fit until we implement ISPN-7351.