ehcache 自定义缓存删除(evictionAdvisor)
************************
注意事项
evictionAdvisor不适用于clustered store
Eviction advisors are not used for clustered storage tiers.
For example, in a cache with a heap tier and clustered storage tier, the heap tier will use the eviction advisor
but the clustered storage tier will evict independently, irrespective of the eviction advisor.
The description below applies to using an eviction advisor for the cache tiers other than a clustered storage tier.
使用自定义的evictionAdvisor会降低缓存清除速度
Please keep in mind that configuring an eviction advisor can slow down eviction:
the more often you advise against eviction, the harder the cache has to work to evict an element when room is required.
如果要删除的缓存被evictionAdvisor拒绝多次,缓存管理器可能会绕过evctionAdvisor直接删除对应的缓存数据
After a certain time, if a cache determines that the configured eviction advisor rejected too many eviction candidates,
the cache can decide to completely bypass the eviction advisor and evict anything it sees fit
************************
相关类与接口
EvictionAdvisor
@FunctionalInterface
public interface EvictionAdvisor<K, V> {
boolean adviseAgainstEviction(K var1, V var2);
//返回true的不删除
}
************************
示例
class CustomEvictionAdvisor implements EvictionAdvisor<Integer,String>{
@Override
public boolean adviseAgainstEviction(Integer integer, String s) {
return integer%2!=0;
}
}
public class Test5 {
public static void main(String[] args){
CacheManager cacheManager= CacheManagerBuilder.newCacheManagerBuilder()
.withCache("custom", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class,String.class,ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(2, EntryUnit.ENTRIES).build()))
.withCache("custom2",CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class,String.class,ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(2,EntryUnit.ENTRIES).build())
.withEvictionAdvisor(new CustomEvictionAdvisor()))
.build(true);
Cache<Integer,String> cache=cacheManager.getCache("custom",Integer.class,String.class);
cache.put(1,"瓜田李下 1");
cache.put(2,"瓜田李下 2");
cache.put(3,"瓜田李下 3");
System.out.println(cache.get(1));
System.out.println(cache.get(2));
System.out.println(cache.get(3));
Cache<Integer,String> cache2=cacheManager.getCache("custom2",Integer.class,String.class);
cache2.put(1,"瓜田李下 1");
cache2.put(2,"瓜田李下 2");
cache2.put(3,"瓜田李下 3");
System.out.println(cache2.get(1));
System.out.println(cache2.get(2));
System.out.println(cache2.get(3));
}
}
****************
控制台输出
23:37:23.547 [main] DEBUG org.ehcache.core.spi.ServiceLocator - Starting 17 Services...
23:37:23.563 [main] DEBUG org.ehcache.core.internal.statistics.DefaultStatisticsService - Starting service
23:37:23.563 [main] DEBUG org.ehcache.core.spi.ServiceLocator - All Services successfully started, 17 Services in 16ms
23:37:23.563 [main] DEBUG org.ehcache.core.EhcacheManager - Creating Cache 'custom' in EhcacheManager.
23:37:23.579 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.Integer> : org.ehcache.impl.serialization.IntegerSerializer@1d8d30f7
23:37:23.579 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.String> : org.ehcache.impl.serialization.StringSerializer@4abdb505
23:37:23.594 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.Integer> : org.ehcache.impl.copy.IdentityCopier@32eebfca
23:37:23.594 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.String> : org.ehcache.impl.copy.IdentityCopier@4e718207
23:37:23.641 [main] DEBUG org.ehcache.impl.internal.store.heap.OnHeapStore - No expiration strategy detected
23:37:23.703 [main] DEBUG class org.ehcache.core.Ehcache-custom - Initialize successful.
23:37:23.719 [main] INFO org.ehcache.core.EhcacheManager - Cache 'custom' created in EhcacheManager.
23:37:23.719 [main] DEBUG org.ehcache.core.EhcacheManager - Creating Cache 'custom2' in EhcacheManager.
23:37:23.719 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.Integer> : org.ehcache.impl.serialization.IntegerSerializer@4313f5bc
23:37:23.719 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.String> : org.ehcache.impl.serialization.StringSerializer@7f010382
23:37:23.719 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.Integer> : org.ehcache.impl.copy.IdentityCopier@1e802ef9
23:37:23.719 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.String> : org.ehcache.impl.copy.IdentityCopier@2b6faea6
23:37:23.719 [main] DEBUG org.ehcache.impl.internal.store.heap.OnHeapStore - No expiration strategy detected
23:37:23.719 [main] DEBUG class org.ehcache.core.Ehcache-custom2 - Initialize successful.
23:37:23.719 [main] INFO org.ehcache.core.EhcacheManager - Cache 'custom2' created in EhcacheManager.
23:37:23.719 [main] DEBUG org.ehcache.core.internal.statistics.DefaultStatisticsService - Moving from UNINITIALIZED to AVAILABLE
23:37:23.719 [main] DEBUG org.ehcache.core.internal.statistics.DefaultStatisticsService - Cache added custom
23:37:23.750 [main] DEBUG org.ehcache.core.internal.statistics.DefaultStatisticsService - Cache added custom2
23:37:23.750 [main] DEBUG org.ehcache.core.EhcacheManager - Initialize successful.
null
瓜田李下 2
瓜田李下 3
瓜田李下 1
null
瓜田李下 3
使用自定义的evictionAdvisor后,key为1的缓存数据保存了