【发布时间】:2019-08-13 16:42:37
【问题描述】:
我正在使用 Hazelcast 3.12 Cache 实现,并尝试配置 Eviction 策略。但是,我注意到当缓存驱逐发生时,我的缓存条目侦听器都没有被触发。
我的缓存定义为:
CacheSimpleConfig cacheSimpleConfig = new CacheSimpleConfig()
.setName(CACHE_NAME)
.setKeyType(String.class.getName())
.setValueType((new String[0]).getClass().getName())
.setStatisticsEnabled(true)
.setReadThrough(true)
.setWriteThrough(true)
.setInMemoryFormat(InMemoryFormat.OBJECT)
.setEvictionConfig(new EvictionConfig()
.setEvictionPolicy(EvictionPolicy.LRU)
.setSize(1000)
.setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT))
.setExpiryPolicyFactoryConfig(
new ExpiryPolicyFactoryConfig(
new TimedExpiryPolicyFactoryConfig(ACCESSED,
new DurationConfig(
ssoSessionTimeoutInSeconds,
TimeUnit.SECONDS))));
hazelcastInstance.getConfig().addCacheConfig(cacheSimpleConfig);
ICache<String, String[]> cache = hazelcastInstance.getCacheManager().getCache(CACHE_NAME);
cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(entryListenerFactory, null, true, false));
我的监听器实现了 4 个 CacheEntry*Listener 接口:
public class UserRolesCacheEntryListener implements CacheEntryExpiredListener<String, String[]>, CacheEntryRemovedListener<String, String[]>, CacheEntryCreatedListener<String, String[]>, CacheEntryUpdatedListener<String, String[]>, HazelcastInstanceAware {
// 所有 4 个 on*() 方法都委托给: 私人无效日志事件(CacheEntryEvent cacheEntryEvent){ String[] value = cacheEntryEvent.getValue() != null ? cacheEntryEvent.getValue() : cacheEntryEvent.getOldValue(); LOG.info("Event[{}:{}] {} => {}", cacheEntryEvent.getEventType().toString(), cacheEntryEvent.getSource().getName(), cacheEntryEvent.getKey(), value);
ICache cache = (ICache)cacheEntryEvent.getSource().unwrap(ICache.class);
LOG.debug("Cache Evictions: {} ", cache.getLocalCacheStatistics().getCacheEvictions());
}
但是,当条目被逐出时,我从未调用任何 on*() 方法。但是,在创建条目时,我确实在日志中看到了以下条目:
2019-08-13 12:27:14 INFO [hz.Hazelcast.event-1] UserRolesCacheEntryListener:70 - Event[CREATED:UserRoles] 950 => [99cedd4c-ee5b-480b-b374-eea2b919a58a, Tue Aug 13 12:27:14 EDT 2019]
2019-08-13 12:27:14 DEBUG [hz.Hazelcast.event-1] UserRolesCacheEntryListener:79 - Cache Evictions: 1
这向我表明,值正在从缓存中逐出,但没有触发任何侦听器。这是预期的行为吗?我原以为会触发“onRemoved()”方法/监听器。
此外,缓存在 950 个条目后显示驱逐是否有原因,即使它设置为 1000 个?
我看到 Hazelcast 有一个 Map 的驱逐侦听器。有没有办法将 Map 的驱逐侦听器用于缓存?
【问题讨论】: