我一直在试图将榛树播放到我的应用程序中,但是正在遇到一种我曾经与Onexpired vs oferemper oferemered的侦听器相关的行为。
理想情况下,每当从缓存中删除一个值时,我都想执行一些代码。我在缓存上配置了有效的策略,并期望我的"侦听器"将在我的缓存值到期后关注,但似乎并非如此。
Hazelcast在从缓存中删除过期的值之后,或仅通过显式cache.remove()
调用?
我的配置是:
hazelcastInstance = HazelcastInstanceFactory.getOrCreateHazelcastInstance(getHazelcastConfig());
// Add cache used by adams
CacheSimpleConfig cacheSimpleConfig = new CacheSimpleConfig()
.setName(CACHE_NAME)
.setKeyType(UserRolesCacheKey.class.getName())
.setValueType((new String[0]).getClass().getName())
.setReadThrough(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(
120,
TimeUnit.SECONDS))));
hazelcastInstance.getConfig().addCacheConfig(cacheSimpleConfig);
ICache<UserRolesCacheKey, String[]> userRolesCache = hazelcastInstance.getCacheManager().getCache(CACHE_NAME);
userRolesCache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(
new UserRolesCacheListenerFactory(), null, false, false));
}
}
}
我的听众很简单:
public class UserRolesCacheListenerFactory implements Factory<CacheEntryListener<UserRolesCacheKey, String[]>> {
@Override
public CacheEntryListener create() {
return new UserRolesCacheEntryListener();
}
}
和:
public class UserRolesCacheEntryListener implements CacheEntryRemovedListener<UserRolesCacheKey, String[]>{
private final static Logger LOG = LoggerFactory.getLogger(UserRolesCacheEntryListener.class);
@Override
public void onRemoved(Iterable<CacheEntryEvent<? extends UserRolesCacheKey, ? extends String[]>> cacheEntryEvents) throws CacheEntryListenerException {
cacheEntryEvents.forEach(this::deleteDBData);
}
我希望在120年代之后的某个时候我的onRemoved
方法将由Hazelcast调用,因为它可以从缓存中删除过期的值,但似乎从来没有。
这是预期的行为吗?我的缓存配置中是否缺少某些东西?
根据JCACHE规范,第8.4节,REMOVED
事件仅用于显式操作。
聆听EXPIRED
事件会更好,但仍然不理想。
请注意规范中的措辞和此处的代码。EXPIRED
事件是实现的依赖性 - 允许缓存提供商永远不要注意到数据已过期,从不删除,因此永远不要生成事件。
Hazelcast确实注意到此处,但这使您需要及时出现事件,您需要取决于实现。