Creating a store by reference cache

In this post under JCache, I will explain with example how to create store by reference cache.

We create a store by reference cache by calling “setStoreByValue” available in “MutableConfiguration” and we need to pass “false” as method argument.

Below is the complete main code for your reference.

Main Class

1  package defaultPackage;
2  
3  import javax.cache.Cache;
4  import javax.cache.CacheManager;
5  import javax.cache.Caching;
6  import javax.cache.configuration.MutableConfiguration;
7  import javax.cache.spi.CachingProvider;
8  
9  public class JCacheDemo17 {
10     public static void main(String[] args) {
11         CachingProvider cachingProvider = Caching.getCachingProvider();
12         CacheManager cacheManager = cachingProvider.getCacheManager();
13         
14         MutableConfiguration<String, String> mutableConfiguration = new MutableConfiguration<>();
15         mutableConfiguration.setStoreByValue(false);
16         
17         Cache<String, String> cache = cacheManager.createCache("cache1", mutableConfiguration);
18 
19         cache.put("key1", "value1");
20         cache.put("key2", "value2");
21         cache.put("key3", "value3");
22         cache.put("key4", "value4");
23         
24         cachingProvider.close();
25     }
26 }

In the above code, at line 14, we are creating an instance of “MutableConfiguration” class. Then at line 15, we call “setStoreByValue” method and pass “false” as method argument.

In this way we can create a Store by reference cache.

Leave a comment