This post explains how to externalize cache configuration to a file instead of configuring them programmatically.
For our example, I will be using ehcache tool.
Below is the ehcache file which describe the configuration information required to create a cache named “ready-cache”.
The key and value data type is String and the entries will be stored on heap with maximum 200 entries.
Ehcache configuration file
<ehcache:config xmlns:ehcache=”http://www.ehcache.org/v3″>
<ehcache:cache alias=”ready-cache”>
<ehcache:key-type>java.lang.String</ehcache:key-type>
<ehcache:value-type>java.lang.String</ehcache:value-type>
<ehcache:heap unit=”entries”>200</ehcache:heap>
</ehcache:cache>
</ehcache:config>
Below is the main code
1 import java.nio.file.Path;
2 import java.nio.file.Paths;
3 import java.util.Iterator;
4
5 import javax.cache.Cache;
6 import javax.cache.CacheManager;
7 import javax.cache.Caching;
8 import javax.cache.Cache.Entry;
9 import javax.cache.spi.CachingProvider;
10
11 public class JCacheDemo14 {
12 public static void main(String[] args) {
13 CachingProvider cachingProvider = Caching.getCachingProvider();
14 Path path = Paths.get("E:\\Projects\\JavaSEConcepts\\JCacheConcepts\\ehcache1.xml");
15 CacheManager cacheManager = cachingProvider.getCacheManager(path.toUri(), JCacheDemo14.class.getClassLoader());
16
17 Cache cache = cacheManager.getCache("ready-cache", String.class, String.class);
18 cache.put("key1", "value1");
19 cache.put("key2", "value2");
20 cache.put("key3", "value3");
21 cache.put("key4", "value4");
22
23 Iterator iterator = cache.iterator();
24 while(iterator.hasNext()) {
25 Entry entry = iterator.next();
26 System.out.println(entry.getKey() + ":" + entry.getValue());
27 }
28
29 cachingProvider.close();
30 }
31 }
Explanation
At line 13 we get cache provider which is ehcache in this case.
At line 14 we get the path to the cache configuration file.
At line 15 we create an instance of CacheManger by passing the path to configuration file, and data type for key and value.
In all the previous posts related to Caching, I used to get an instance of CacheManager using the below code snippet.
CacheManager cacheManager = cachingProvider.getCacheManager();
The difference between the above code snippet and the code at line 15 is that
getCacheManager() uses default configuration provided by ehcache which is “jsr107-default-config”.
The default configuration doesn’t create any caches. We create cache programmatically which was explained in previous posts.
In our example we use configuration file provided at path “E:\Projects\JavaSEConcepts\JCacheConcepts\ehcache1.xml”.
The CacheManager instance created at line 15 will be always returned whenever we call getCacheManager with same file uri and classloader.
If the instance is closed a new instance is created and returned.
The file contains all the information needed by ehcache to create a cache. All we need to do programmatically, is to add entries to the cache.
We access the cache mentioned in the configuration file using the below code snippet
Cache cache = cacheManager.getCache("ready-cache", String.class, String.class);
getCache method returns an instance of cache or returns null if the named cache (mentioned in arguments) doesn’t exist.
We also need to pass the data type of key and value to get the cache.
Output
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
key1:value1
key2:value2
key3:value3
key4:value4