This post explains EternalExpiryPolicy with simple example.
EternalExpiryPolicy is an expiry policy used to inform cache to never remove entries in the cache.
Below is an example of how to use it.
Main Code
1 import java.util.Iterator;
2
3 import javax.cache.Cache;
4 import javax.cache.Cache.Entry;
5 import javax.cache.CacheManager;
6 import javax.cache.Caching;
7 import javax.cache.configuration.Factory;
8 import javax.cache.configuration.MutableConfiguration;
9 import javax.cache.expiry.EternalExpiryPolicy;
10 import javax.cache.expiry.ExpiryPolicy;
11 import javax.cache.spi.CachingProvider;
12
13 public class JCacheDemo14 {
14 public static void main(String[] args) {
15 CachingProvider cachingProvider = Caching.getCachingProvider();
16 CacheManager cacheManager = cachingProvider.getCacheManager();
17
18 Factory eternalExpiryPolicyFactory = EternalExpiryPolicy.factoryOf();
19
20 MutableConfiguration mutableConfiguration = new MutableConfiguration();
21 mutableConfiguration.setTypes(String.class, String.class);
22 mutableConfiguration.setExpiryPolicyFactory(eternalExpiryPolicyFactory);
23
24 Cache cache = cacheManager.createCache("cache1", mutableConfiguration);
25 cache.put("key1", "value1");
26 cache.put("key2", "value2");
27 cache.put("key3", "value3");
28 cache.put("key4", "value4");
29
30 System.out.println("Going through loop");
31 Iterator iterator = cache.iterator();
32 while(iterator.hasNext()) {
33 Entry entry = iterator.next();
34 System.out.println(entry.getKey() + ":" + entry.getValue());
35 }
36 System.out.println("Done going through loop");
37 }
38 }
In the above code, at line 18, I create a factory instance that will be used to create an instance of EternalExpiryPolicy.
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.
Going through loop
key2:value2
key1:value1
key4:value4
key3:value3
Done going through loop