Creating a custom expiry policy factory

This post explains how to create a custom expiry policy for the cache.

Below is the example

Custom Expiry Policy


1  import java.util.concurrent.TimeUnit;
2  
3  import javax.cache.expiry.Duration;
4  import javax.cache.expiry.ExpiryPolicy;
5  
6  public class CustomTouchedExpiryPolicy implements ExpiryPolicy {
7   @Override
8   public Duration getExpiryForAccess() {
9       Duration duration = new Duration(TimeUnit.MINUTES, 2);
10      return duration;
11  }
12 
13  @Override
14  public Duration getExpiryForCreation() {
15      return Duration.ONE_MINUTE;
16  }
17 
18  @Override
19  public Duration getExpiryForUpdate() {
20      Duration duration = new Duration(TimeUnit.MINUTES, 3);
21      return duration;
22  }
23 }

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.FactoryBuilder;
9  import javax.cache.configuration.MutableConfiguration;
10 import javax.cache.expiry.ExpiryPolicy;
11 import javax.cache.spi.CachingProvider;
12 
13 public class JCacheDemo16 {
14  public static void main(String[] args) throws Exception {
15      CachingProvider cachingProvider = Caching.getCachingProvider();
16      CacheManager cacheManager = cachingProvider.getCacheManager();
17 
18      Factory customExpiryPolicyfactory = FactoryBuilder.factoryOf(CustomTouchedExpiryPolicy.class);
19      
20      MutableConfiguration mutableConfiguration = new MutableConfiguration();
21      mutableConfiguration.setTypes(String.class, String.class);
22      mutableConfiguration.setExpiryPolicyFactory(customExpiryPolicyfactory);
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      ExpiryPolicy expiryPolicy = (ExpiryPolicy)customExpiryPolicyfactory.create();
31      System.out.println("Creation duration: " + expiryPolicy.getExpiryForCreation().getDurationAmount());
32      System.out.println("Update duration: " + expiryPolicy.getExpiryForUpdate().getDurationAmount());
33      System.out.println("Access duration: " + expiryPolicy.getExpiryForAccess().getDurationAmount());
34      
35      System.out.println("Waiting for 0.3 minutes");
36      Thread.sleep(20000);
37      System.out.println("After 0.3 minutes update key1 and then loop through cache entry. All the items added previous will be present.");
38      
39      cache.put("key1", "value5");
40      
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 2.1 minutes");
50      Thread.sleep(126000);
51      System.out.println("After 2.1 minutes again loop through cache entry. Only key1 entry is present.");
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 3.4 minutes");
64      Thread.sleep(204000);
65      System.out.println("After 3.4 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 }

Explanation

First we create a custom expiry policy class named “CustomTouchedExpiryPolicy” which implements the interface “ExpiryPolicy”. We should implement the interface
methods as shown at line 8, 14, and 19 of “CustomTouchedExpiryPolicy”.

Next we use it the main code. First we need to create a factory instance which will create an instance of “CustomTouchedExpiryPolicy” whenever we call create method on the Factory instance. We create an instance of Factory class, by using static method of FactoryBuilder. Refer to line 18.

Next we set the factory instance to an instance of MutableConfiguration class which will be used to configure the cache.

Remaining code is similar to code in other posts.

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: 3
Access duration: 2
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 2.1 minutes
After 2.1 minutes again loop through cache entry. Only key1 entry is present.
Going through loop
key1:value5
Done going through loop
Waiting for another 3.4 minutes
After 3.4 minutes loop through cache entry and nothing will be there.
Going through loop
Done going through loop

Leave a Reply