Multiple Ehcache Manager Configurations
The simplest use of the multi-configuration features is to embed multiple cache manager configurations in a single XML file:
<multi:configurations
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:multi='http://www.ehcache.org/v3/multi'
xsi:schemaLocation='http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd
http://www.ehcache.org/v3/multi http://www.ehcache.org/schema/ehcache-multi.xsd'> //1
<multi:configuration identity="foo-manager"> //2
<config>
<cache alias="foo">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<resources>
<heap unit="entries">20</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache>
</config>
</multi:configuration>
<multi:configuration identity="bar-manager">
<config>
<cache alias="bar">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<resources>
<heap unit="entries">20</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache>
</config>
</multi:configuration>
</multi:configurations>
1 | A top-level <configurations> container with namespace declarations for the multi and core schemas. |
2 | Each Ehcache configuration is embedded inside a configuration tag with a required (unique) identity attribute. |
These embedded configurations can then be retrieved via an XmlMultiConfiguration instance built from the XML document.
XmlMultiConfiguration multipleConfiguration = XmlMultiConfiguration
.from(getClass().getResource("/configs/docs/multi/multiple-managers.xml")) //1
.build(); //2
Configuration fooConfiguration = multipleConfiguration.configuration("foo-manager"); //3
1 | The XmlMultiConfiguration is assembled from the XML resource. |
2 | Once assembled, the configuration is built. |
3 | You can retrieve a specific Configuration instance by using its identity. |