Terracotta 10.7 | Ehcache API Developer Guide | Configuring a CacheManager Using XML | Configuring Storage Tiers using XML
 
Configuring Storage Tiers using XML
As an alternative to programmatic configuration, a CacheManager can also be configured using XML, as illustrated in this example:

<config>

<cache alias="foo"> <!--1-->
<key-type>java.lang.String</key-type> <!--2-->
<resources>
<heap unit="entries">2000</heap> <!--3-->
<offheap unit="MB">500</offheap> <!--4-->
</resources>
</cache>

<cache-template name="myDefaults"> <!--5-->
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>

<cache alias="bar" uses-template="myDefaults"> <!--6-->
<key-type>java.lang.Number</key-type>
</cache>

<cache alias="simpleCache" uses-template="myDefaults" /> <!--7-->

</config>
1. Declares a Cache aliased to foo.
2. The keys of foo are declared as type String.
Since the value type is not specified, the values will be of type java.lang.Object.
3. foo is declared to hold up to 2,000 entries on heap ...
4. ... as well as up to 500 MB of off-heap memory before the Cache starts evicting
5. <cache-template> elements create an abstract configuration that can be extended by further <cache> configurations
6. bar is an example for such a Cache.
bar uses the <cache-template> named myDefaults and overrides its key-type to a wider type.
7. simpleCache is another such Cache.
simpleCache uses myDefaults configuration for its sole CacheConfiguration.
The schema and format of the XML is explained in detail in the following section The XML Schema Definition.
The type XmlConfiguration allows for parsing an XML configuration:

URL myUrl = getClass().getResource("/my-config.xml"); // <1>
Configuration xmlConfig = new XmlConfiguration(myUrl); // <2>
CacheManager myCacheManager =
CacheManagerBuilder.newCacheManager(xmlConfig); // <3>
As the steps are ...
1. Obtain a URL to the location of the XML file.
2. Instantiate an XmlConfiguration passing the URL of the XML file.
3. Use the static
org.ehcache.config.builders.CacheManagerBuilder
.newCacheManager(org.ehcache.config.Configuration)
creates the CacheManager instance using the Configuration from the XmlConfiguration.