JCache TouchedExpiryPolicy

This post explains TouchedExpiryPolicy with simple example.

TouchedExpiryPolicy is an expiry policy used to inform cache to remove entries which has exceeded its presence since last modification (which includes creation, access 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.TouchedExpiryPolicy;
12 import javax.cache.spi.CachingProvider;
13 
14 public class JCacheDemo7 {
15  public static void main(String[] args) throws Exception {
16      CachingProvider cachingProvider = Caching.getCachingProvider();
17      CacheManager cacheManager = cachingProvider.getCacheManager();
18 
19      Factory expiryPolicyFactory = TouchedExpiryPolicy.factoryOf(Duration.ONE_MINUTE);
20      
21      MutableConfiguration mutableConfiguration = new MutableConfiguration();
22      mutableConfiguration.setTypes(String.class, String.class);
23      mutableConfiguration.setExpiryPolicyFactory(expiryPolicyFactory);
24      
25      Cache cache = cacheManager.createCache("cache1", mutableConfiguration);
26      cache.put("key1", "value1");
27      cache.put("key2", "value2");
28      cache.put("key3", "value3");
29      cache.put("key4", "value4");
30      
31      ExpiryPolicy expiryPolicy = expiryPolicyFactory.create();
32      System.out.println("Creation duration: " + expiryPolicy.getExpiryForCreation().getDurationAmount());
33      System.out.println("Update duration: " + expiryPolicy.getExpiryForUpdate().getDurationAmount());
34      System.out.println("Access duration: " + expiryPolicy.getExpiryForAccess().getDurationAmount());
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 then loop through cache entry. All the items added previous will be present.");
39      
40      cache.put("key1", "value5");
41      
42      System.out.println("Going through loop");
43      Iterator<Entry> iterator = cache.iterator();
44      while(iterator.hasNext()) {
45          Entry entry = iterator.next();
46          System.out.println(entry.getKey() + ":" + entry.getValue());
47      }
48      System.out.println("Done going through loop");
49      
50      System.out.println("Waiting for another 0.8 minutes");
51      Thread.sleep(50000);
52      System.out.println("After 0.8 minutes again loop through cache entry. All the entries are present.");
53      
54      System.out.println("Going through loop");
55      iterator = cache.iterator();
56      while(iterator.hasNext()) {
57          Entry entry = iterator.next();
58          System.out.println(entry.getKey() + ":" + entry.getValue());
59      }
60      System.out.println("Done going through loop");
61      
62      System.out.println("Waiting for another 1 minutes");
63      Thread.sleep(70000);
64      System.out.println("After 1 minutes loop through cache entry and nothing will be there.");
65      
66      System.out.println("Going through loop");
67      iterator = cache.iterator();
68      while(iterator.hasNext()) {
69          Entry entry = iterator.next();
70          System.out.println(entry.getKey() + ":" + entry.getValue());
71      }
72      System.out.println("Done going through loop");
73  }
74 }

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: 1
Waiting for 0.3 minutes
After 0.3 minutes update key1 and then loop through cache entry. All the items added previous will be present.
Going through loop
key1:value5
key2:value2
key3:value3
key4:value4
Done going through loop
Waiting for another 0.8 minutes
After 0.8 minutes again loop through cache entry. All the entries are present.
Going through loop
key1:value5
key2:value2
key3:value3
key4:value4
Done going through loop
Waiting for another 1 minutes
After 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 TouchedExpiryPolicy (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 TouchedExpiryPolicy 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, access 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 or accessed, 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 all the enties since each entries duration is increased by 1 because of update and access operation.

We wait for another 1 minute. After 1 minute when we loop through the cache entry and since the entries have exceeded their presence, all 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