JCache Expiry Policy Example

This post explains about JCache’s Expiry policy with an example.

Cache’s Expiry policy is used to inform the caching provider when to remove an entry.

The below code uses JCache’s CreatedExpiryPolicy as an example.

CreatedExpiryPolicy is used to inform the cache provider to remove the entry after a specified time since the entry’s addition to the cache.

We need to create an instance of CreatedExpiryPolicyFactory using the below code snippet. Refer to line 19 in the main code.
Factory expiryPolicyFactory = CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE);

Where Duration.ONE_MINUTE is the duration that we want the entry to be present since its creation.

This factory instance is set to an instance of MutableConfiguration, which will be used to configure the cache when it is being created, as shown below. Refer to line 23 in the main code.
mutableConfiguration.setExpiryPolicyFactory(expiryPolicyFactory);

The below code creates a CreatedExpiryPolicy with a duration of one minute. Then we add four entries and loop through cache entries before going to sleep for 2 minutes. After 2 minutes we again loop through the cache entries and this time, the entries will be empty.

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.CreatedExpiryPolicy;
10 import javax.cache.expiry.Duration;
11 import javax.cache.expiry.ExpiryPolicy;
12 import javax.cache.spi.CachingProvider;
13 
14 public class JCacheDemo5 {
15  public static void main(String[] args) throws InterruptedException {
16      CachingProvider cachingProvider = Caching.getCachingProvider();
17      CacheManager cacheManager = cachingProvider.getCacheManager();
18      
19      Factory expiryPolicyFactory = CreatedExpiryPolicy.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      System.out.println("Going through loop");
32      Iterator<Entry> iterator = cache.iterator();
33      while(iterator.hasNext()) {
34          Entry entry = iterator.next();
35          System.out.println(entry.getKey() + ":" + entry.getValue());
36      }
37      
38      System.out.println("Before sleep");
39      Thread.sleep(120000);
40      System.out.println("After sleep");
41      
42      cache = cacheManager.getCache("cache1", String.class, String.class);
43      System.out.println("Going through loop");
44      iterator = cache.iterator();
45      while(iterator.hasNext()) {
46          Entry entry = iterator.next();
47          if(entry != null) {
48              System.out.println(entry.getKey() + ":" + entry.getValue());    
49          }
50      }
51      
52      cachingProvider.close();
53  }
54 }

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
Before sleep
After sleep
Going through loop

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