This post explains different ways in which we can remove entries in a cache using JCache api. The JCache api provides the below methods
1) boolean remove(K key) –> remove an entry with key matching the method parameter
2) boolean remove(K key, V oldValue) –> removes an entry with matching key and value
3) void removeAll() –> removes all entries in the cache
4) void removeAll(Set keys) –> removes all entries in the caches whose keys match with the keys listed in the set.
Main code
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.cache.Cache;
import javax.cache.Cache.Entry;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;
public class JCacheDemo3 {
public static void main(String[] args) {
CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();
MutableConfiguration mutableConfiguration = new MutableConfiguration();
mutableConfiguration.setTypes(String.class, String.class);
mutableConfiguration.setStatisticsEnabled(true);
Cache cache = cacheManager.createCache("cache1", mutableConfiguration);
cache.put("key1", "value1");
cache.put("key2", "value2");
cache.put("key3", "value3");
cache.put("key4", "value4");
Iterator iterator = cache.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("----------Remove with key example------------");
cache.remove("key1");
iterator = cache.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------Remove with key and value example-------------");
cache.remove("key2", "value2");
iterator = cache.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------key1 and key2 added again-------------");
cache.put("key1", "value1");
cache.put("key2", "value2");
Set keys = new HashSet(0);
keys.add("key1");
keys.add("key2");
System.out.println("--------Remove all with set example--------------");
cache.removeAll(keys);
iterator = cache.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------Remove all example-------------");
cache.removeAll();
iterator = cache.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
cachingProvider.close();
}
}
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.
key2:value2
key1:value1
key4:value4
key3:value3
———-Remove with key example————
key2:value2
key4:value4
key3:value3
———Remove with key and value example————-
key4:value4
key3:value3
———key1 and key2 added again————-
——–Remove all with set example————–
key4:value4
key3:value3
———Remove all example————-