use of org.ehcache.config.builders.CacheManagerBuilder in project CzechIdMng by bcvsolutions.
the class ClusteredEhCacheConfiguration method ehCacheManager.
/**
* Defines clustered {@link CacheManager} using Terracotta server.
*
* @param terracotaUrl a list of IP addresses with ports (IP_ADDR:PORT)
* @param terracotaResourceName name of server resource to connect
* @param terracotaResourcePoolName name od server resource pool name
* @param terracotaResourcePoolSize size of server resource pool in MB
* @param idMCacheConfigurations a list of {@link IdMCacheConfiguration} defined in container
* @return CacheManager with distributed capabilities
*/
@Bean
@Qualifier("jCacheManager")
@ConditionalOnProperty(value = TERRACOTA_URL_PROPERTY)
@ConditionalOnMissingBean
public CacheManager ehCacheManager(@Value("${" + TERRACOTA_URL_PROPERTY + "}") String terracotaUrl, @Value("${" + TERRACOTA_RESOURCE_NAME_PROPERTY + "}") String terracotaResourceName, @Value("${" + TERRACOTA_RESOURCE_POOL_NAME_PROPERTY + "}") String terracotaResourcePoolName, @Value("${" + TERRACOTA_RESOURCE_POOL_SIZE_PROPERTY + "}") int terracotaResourcePoolSize, @Autowired List<IdMCacheConfiguration> idMCacheConfigurations) {
CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder().with(ClusteringServiceConfigurationBuilder.cluster(parseServerAddresses(terracotaUrl), "default").autoCreate(server -> server.defaultServerResource(terracotaResourceName).resourcePool(terracotaResourcePoolName, terracotaResourcePoolSize, MemoryUnit.MB, terracotaResourceName))).withSerializer(CacheObjectWrapper.class, CacheWrapperSerializer.class).withSerializer(SerializableCacheObjectWrapper.class, SerializableCacheWrapperSerializer.class);
PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true);
// create caches using IdMCacheConfiguration instances
if (!CollectionUtils.isEmpty(idMCacheConfigurations)) {
for (IdMCacheConfiguration config : idMCacheConfigurations) {
cacheManager.createCache(config.getCacheName(), toConcreteConfiguration(config, terracotaResourcePoolName));
}
}
// get CacheManager (Jcache) with above updated configuration
final EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider();
return ehcacheCachingProvider.getCacheManager(ehcacheCachingProvider.getDefaultURI(), cacheManager.getRuntimeConfiguration());
}
Aggregations