JCache ModifiedExpiryPolicy

This post explains ModifiedExpiryPolicy with simple example.

ModifiedExpiryPolicy is an expiry policy used to inform cache to remove entries which has exceeded its presence since last modification (which includes creation and update)

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.Duration;
10 import javax.cache.expiry.ExpiryPolicy;
11 import javax.cache.expiry.ModifiedExpiryPolicy;
12 import javax.cache.spi.CachingProvider;
13 
14 public class JCacheDemo6 {
15  public static void main(String[] args) throws Exception {
16      CachingProvider cachingProvider = Caching.getCachingProvider();
17      CacheManager cacheManager = cachingProvider.getCacheManager();
18 
19      Factory expiryPolicyFactory2 = ModifiedExpiryPolicy.factoryOf(Duration.ONE_MINUTE);
20      
21      MutableConfiguration mutableConfiguration = new MutableConfiguration();
22      mutableConfiguration.setTypes(String.class, String.class);
23      mutableConfiguration.setExpiryPolicyFactory(expiryPolicyFactory2);
24      
25      Cache cache = cacheManager.createCache("cache2", mutableConfiguration);
26      cache.put("key1", "value1");
27      cache.put("key2", "value2");
28      cache.put("key3", "value3");
29      cache.put("key4", "value4");
30      
31      ExpiryPolicy expiryPolicy2 = expiryPolicyFactory2.create();
32      System.out.println("Creation duration: " + expiryPolicy2.getExpiryForCreation().getDurationAmount());
33      System.out.println("Update duration: " + expiryPolicy2.getExpiryForUpdate().getDurationAmount());
34      System.out.println("Access duration: " + expiryPolicy2.getExpiryForAccess());
35      
36      System.out.println("Waiting for 0.3 minutes");
37      Thread.sleep(20000);
38      System.out.println("After 0.3 minutes update key1 and loop through cache entry. All the items added previous will be present.");
39      
40      cache.put("key1", "value5");
41      System.out.println("Going through loop");
42      Iterator<Entry> iterator = cache.iterator();
43      while(iterator.hasNext()) {
44          Entry entry = iterator.next();
45          System.out.println(entry.getKey() + ":" + entry.getValue());
46      }
47      System.out.println("Done going through loop");
48      
49      System.out.println("Waiting for another 0.8 minutes");
50      Thread.sleep(50000);
51      System.out.println("After 0.8 minutes again loop through cache entry and only one entry will be there. Remaining entries are removed as they are expired.");
52      
53      System.out.println("Going through loop");
54      iterator = cache.iterator();
55      while(iterator.hasNext()) {
56          Entry entry = iterator.next();
57          if(entry != null) {
58              System.out.println(entry.getKey() + ":" + entry.getValue());
59          }
60      }
61      System.out.println("Done going through loop");
62      
63      System.out.println("Waiting for another 1 minutes");
64      Thread.sleep(70000);
65      System.out.println("After 1 minutes loop through cache entry and nothing will be there.");
66      
67      System.out.println("Going through loop");
68      iterator = cache.iterator();
69      while(iterator.hasNext()) {
70          Entry entry = iterator.next();
71          if(entry != null) {
72              System.out.println(entry.getKey() + ":" + entry.getValue());
73          }
74      }
75      System.out.println("Done going through loop");
76  }
77 }

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.
Creation duration: 1
Update duration: 1
Access duration: null
Waiting for 0.3 minutes
After 0.3 minutes update key1 and loop through cache entry. All the items added previous will be present.
Going through loop
key2:value2
key1:value5
key4:value4
key3:value3
Done going through loop
Waiting for another 0.8 minutes
After 0.8 minutes again loop through cache entry and only one entry will be there. Remaining entries are removed as they are expired.
Going through loop
key1:value5
Done going through loop
Waiting for another 1 minutes
After another 1 minutes loop through cache entry and nothing will be there.
Going through loop
Done going through loop

Explanation

At line 19 we create an factory instance which in turn will be used to create instance of ModifiedExpiryPolicy (refer to line 31). The duration for which a cache entry is allowed to stay in cache is passed as parameter to factoryOf method, which in this case is 1 minute.

Once an instance of ModifiedExpiryPolicy is created, we can print the duration of each operation (create, update, and access) as done at line 32 to 34. From the output we can the see the duration of creation and update is 1 minute. So when entry is created it will present in the cache for 1 minute. If within 1 minute an entry is updated, the duration of that entry is increased by 1 minute.

At line 37 we wait for 0.3 minutes and after 0.3 minutes, we update “key1” and loop through the entries. You will see from the output all the entries are present and value of “key1” is updated.

Then we wait for 0.8 minutes and after 0.8 minutes we loop through the entries. When we go through the entries, we find only one entry which is “key1” as the others have exceeded their presence (which is 1 minute since its creation) and hence removed.

We wait for another 1 minute. After 1 minute when we loop through the cache entry and since the entry “key1” has exceeded their presence, it will be removed as shown in the output.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s