Terracotta 10.15 | Ehcache API Developer Guide | Configuring a CacheManager Using XML | Programmatic configuration to XML
 
Programmatic configuration to XML
You can now create an XML configuration file from a cache manager configuration. To do so, you have to construct an XmlConfiguration object by passing Configuration to the cache manager. The string representation of the XmlConfiguration object will be the XML equivalent of the configuration of that cache manager.
XmlConfiguration xmlConfiguration = new
XmlConfiguration(getClass().getResource("/configs/docs/template-sample.xml"));
CacheConfigurationBuilder<Long, String> configurationBuilder =
xmlConfiguration.newCacheConfigurationBuilderFromTemplate("example", Long.class, String.class); //1
configurationBuilder = configurationBuilder.withResourcePools(ResourcePoolsBuilder.heap(1000)); //2
1
Creates a builder, inheriting the capacity constraint of 200 entries.
2
You can override the inherent properties by providing a different value prior to building the CacheConfiguration
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence(tmpDir.newFile("myData")))
.withCache("threeTieredCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true))
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(20)))
).build(false);

Configuration configuration = cacheManager.getRuntimeConfiguration();
XmlConfiguration xmlConfiguration = new XmlConfiguration(configuration); //1
String xml = xmlConfiguration.toString(); //2
1
Instantiate an XmlConfiguration object and passing Configuration as a paramater to the cache manager.
2
Retrieve the XML representation using the `toString` method.
Not every programmatic configuration can be translated to XML in this manner. Translation is not supported if the cache manager configuration contains a cache that has any of the following configured:
*EvictionAdvisor
*A custom ExpiryPolicy other than timeToLiveExpiration or timeToIdleExpiration from ExpiryPolicyBuilder
*Using an instance of the following instead of their classes:
*Serializer
*Copier
*CacheLoaderWriter
*CacheEventListener
*ResilienceStrategy