Terracotta Ehcache 10.5 | Ehcache API Developer Guide | Configuring a CacheManager Using XML | XML Programmatic Parsing
 
XML Programmatic Parsing
The following section goes through the steps and possibilities of automatically configuring a CacheManager using XML.
Note: If you are obtaining your CacheManager through API calls based on JSR-107, what follows is done automatically when invoking javax.cache.spi.CachingProvider.getCacheManager(java.net.URI, java.lang.ClassLoader).

final URL myUrl =
getClass().getResource("/configs/docs/getting-started.xml"); // <1>
XmlConfiguration xmlConfig = new XmlConfiguration(myUrl); // <2>
CacheManager myCacheManager =
CacheManagerBuilder.newCacheManager(xmlConfig); // <3>
myCacheManager.init(); // <4>
1. Obtain a URL to your XML file's location
2. Instantiate an XmlConfiguration passing the URL of the XML file to it.
3. Create your CacheManager instance using the Configuration from the XmlConfiguration.
4. Initialize the CacheManager before it is used.
We can also use <cache-template> declared in the XML file to seed instances of CacheConfigurationBuilder. In order to use a <cache-template> element from an XML file, the XML file contains the following XML fragment:

<cache-template name="example">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>
Creating a CacheConfigurationBuilder of that example <cache-template> element would be done as follows:

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. The inherent properties can be overridden by simply providing a different value prior to building the CacheConfiguration.

Copyright © 2010-2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.