In this post under JCache, I will explain with example the purpose of “AccessedExpiryPolicy” and how to use it
AccessedExpiryPolicy is an expiry policy used to inform cache to remove entries which has exceeded its presence based on last accessed.
Access involves creation and access of cache entries but not update.
Below the main code that shows how to use “AccessedExpiryPolicy” class.
Main Code
1 package defaultPackage;
2
3 import java.util.Iterator;
4
5 import javax.cache.Cache;
6 import javax.cache.Cache.Entry;
7 import javax.cache.CacheManager;
8 import javax.cache.Caching;
9 import javax.cache.configuration.Factory;
10 import javax.cache.configuration.MutableConfiguration;
11 import javax.cache.expiry.AccessedExpiryPolicy;
12 import javax.cache.expiry.Duration;
13 import javax.cache.expiry.ExpiryPolicy;
14 import javax.cache.spi.CachingProvider;
15
16 public class JCacheDemo15 {
17 public static void main(String[] args) throws Exception {
18 CachingProvider cachingProvider = Caching.getCachingProvider();
19 CacheManager cacheManager = cachingProvider.getCacheManager();
20
21 Factory<ExpiryPolicy> accessedExpiryPolicyFactory = AccessedExpiryPolicy.factoryOf(Duration.ONE_MINUTE);
22
23 MutableConfiguration<String, String> mutableConfiguration = new MutableConfiguration<>();
24 mutableConfiguration.setExpiryPolicyFactory(accessedExpiryPolicyFactory);
25
26 Cache<String, String> cache = cacheManager.createCache("cache1", mutableConfiguration);
27
28 cache.put("key1", "value1");
29 cache.put("key2", "value2");
30 cache.put("key3", "value3");
31 cache.put("key4", "value4");
32
33 ExpiryPolicy expiryPolicy = accessedExpiryPolicyFactory.create();
34 System.out.println("Creation duration: " + expiryPolicy.getExpiryForCreation().getDurationAmount());
35 System.out.println("Update duration: " + expiryPolicy.getExpiryForUpdate());
36 System.out.println("Access duration: " + expiryPolicy.getExpiryForAccess().getDurationAmount());
37
38 System.out.println("Waiting for 0.3 minutes");
39 Thread.sleep(20000);
40 System.out.println("After 0.3 minutes access key1.");
41
42 String result = cache.get("key1");
43
44 System.out.println("Waiting for another 0.8 minutes");
45 Thread.sleep(50000);
46 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.");
47
48 Iterator<Cache.Entry<String, String>> iterator = cache.iterator();
49 while(iterator.hasNext()) {
50 Entry<String, String> entry = iterator.next();
51 System.out.println(entry.getKey() + ":" + entry.getValue());
52 }
53
54 System.out.println("Waiting for another 1 minutes");
55 Thread.sleep(70000);
56 System.out.println("After 1 minutes loop through cache entry and nothing will be there.");
57
58 iterator = cache.iterator();
59 while(iterator.hasNext()) {
60 Entry<String, String> entry = iterator.next();
61 System.out.println(entry.getKey() + ":" + entry.getValue());
62 }
63
64 cachingProvider.close();
65 }
66 }
In the above code, at line 18, we get an instance of “CachingProvider”.
At line 19, we get an instance of “CacheManager”.
At line 21, we create an factory instance for “AccessedExpiryPolicy” which in turn will be used to create an instance of “AccessedExpiryPolicy”.
The duration for which a cache entry is allowed in cache is passed as paramter to factoryOf method, which in this case is 1 minute.
From 26 to 31, we create a cache and populate it at T minutes.
At line 33, we create an instance of “AccessedExpiryPolicy”.
From line 34 to 36, we print the duration of each operation (create, update, and access).
From the output we can see the duration of creation and access is 1 minute, whereas update is null.
So when an entry is created it will present in the cache for 1 minute. If within 1 minute an entry is accessed, the duration of that entry is increased by another 1 minute.
At line 39, we wait for 0.3 minute and after 0.3 minutes we access “key1”. As a result it will present in cache beyond 1 minute i.e., beyond T minutes since its creation.
So if T is 1 minute, (T – 0.3) will be 0.7 minute and T1 will be 1.7 minute i.e, beyond 1 minute.
At line 45, we wait for another 0.8 minute and after 0.8 minutes, i.e., (0.7 + 0.8) = 1.5 minutes we print the cache contents.
As you can see from the output, all the cache entries except “key1” are removed.
After waiting for another (1.5 + 1) minute when we loop through cache entries, the cache will be empty.
Because the last entry “key1” is removed after 1.7 minutes.
Below is the output for your reference.
Output
[main] INFO org.ehcache.core.EhcacheManager - Cache 'cache1' created in EhcacheManager.
Creation duration: 1
Update duration: null
Access duration: 1
Waiting for 0.3 minutes
After 0.3 minutes access key1.
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.
key1:value1
Waiting for another 1 minutes
After 1 minutes loop through cache entry and nothing will be there.
[main] INFO org.ehcache.core.EhcacheManager - Cache 'cache1' removed from EhcacheManager.
In this way “AccessedExpiryPolicy” is used.