15.2.2.2. Override the Creation of the Embedded Cache Manager
After a producer method is annotated, this method will be called when creating an EmbeddedCacheManager
, as follows:
public class Config { @Produces @ApplicationScoped public EmbeddedCacheManager defaultEmbeddedCacheManager() { Configuration cfg = new ConfigurationBuilder() .eviction() .strategy(EvictionStrategy.LRU) .maxEntries(150) .build(); return new DefaultCacheManager(cfg); } }
@ApplicationScoped
annotation specifies that the method is only called once.
The following configuration can be used to create an EmbeddedCacheManager
that can create clustered caches.
public class Config { @Produces @ApplicationScoped public EmbeddedCacheManager defaultClusteredCacheManager() { GlobalConfiguration g = new GlobalConfigurationBuilder() .clusteredDefault() .transport() .clusterName("InfinispanCluster") .build(); Configuration cfg = new ConfigurationBuilder() .eviction() .strategy(EvictionStrategy.LRU) .maxEntries(150) .build(); return new DefaultCacheManager(g, cfg); } }
The method annotated with @Produces
in the non clustered method generates Configuration
objects. The methods in the clustered cache example annonated with @Produces
generate EmbeddedCacheManager
objects.
EmbeddedCacheManager
and injects it into the code at runtime.
... @Inject EmbeddedCacheManager cacheManager; ...