In this post, I am going to explain how to configure a cache using xml configuration.
How to define a cache using xml?
<cache> element is used to define a cache object.
Example
<cache alias="empCache1">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<resources>
<heap unit="entries">3000</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache>
Above snippet define a cache aliased to empCache1. The keys and values of empCache1 are declared as type Long and String, if not specified, the default is java.lang.Object. empCache1 is declared to hold up to 2,000 entries on heap, as well as up to 10 MB of off-heap memory before it starts evicting.
Define a cache using a template
You can define the cache configuration using <cache-template> element and later <cache> configuration can use this template configurations.
<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>
<cache alias="empCache3" uses-template="myDefaults" />
You can even override the configurations of template at cache element level.
<cache alias="empCache2" uses-template="myDefaults">
<key-type>java.lang.Integer</key-type>
</cache>
How to get CacheManager from xml config file?
Below snippet get the instance of CacheManager from the configuraitons defined in cacheConfig.xml file.
URL myUrl = CacheViaXMLConfig.class.getResource("/cacheConfig.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
myCacheManager.init();
Find the below working application.
Define cacheConfig.xml file under src/main/resources folder.
cacheConfig.xml
<config xmlns='http://www.ehcache.org/v3'>
<cache alias="empCache1">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<resources>
<heap unit="entries">3000</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache>
<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>
<cache alias="empCache2" uses-template="myDefaults">
<key-type>java.lang.Integer</key-type>
</cache>
<cache alias="empCache3" uses-template="myDefaults" />
</config>
Define CacheViaXMLConfig class.
CacheViaXMLConfig.java
package com.sample.app;
import java.net.URL;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.Configuration;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;
public class CacheViaXMLConfig {
public static void main(String[] args) {
URL myUrl = CacheViaXMLConfig.class.getResource("/cacheConfig.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
myCacheManager.init();
Cache<Long, String> empCache1 = myCacheManager.getCache("empCache1", Long.class, String.class);
Cache<Integer, String> empCache2 = myCacheManager.getCache("empCache2", Integer.class, String.class);
Cache<Long, String> empCache3 = myCacheManager.getCache("empCache3", Long.class, String.class);
empCache1.put(1L, "Krishna,34");
empCache2.put(1, "Krishna,34");
empCache3.put(1L, "Krishna,34");
System.out.printf("empCache1.get(%d) : %s\n", 1L, empCache1.get(1L));
System.out.printf("empCache2.get(%d) : %s\n", 1, empCache2.get(1));
System.out.printf("empCache3.get(%d) : %s\n", 1L, empCache3.get(1L));
}
}
Output
empCache1.get(1) : Krishna,34
empCache2.get(1) : Krishna,34
empCache3.get(1) : Krishna,34
No comments:
Post a Comment