【问题标题】:Does Hazelcast trigger the onRemoved listener for expired cache values?Hazelcast 是否为过期缓存值触发 onRemoved 侦听器?
【发布时间】:2019-05-02 01:11:24
【问题描述】:

我一直在尝试将 Hazelcast 集成到我的应用程序中,但在使用 onExpired 与 onRemoved 侦听器时遇到了我没有预料到的行为。

理想情况下,我希望在从缓存中删除某个值时执行一些代码。我在缓存上配置了过期策略,并希望我的 onRemoved 监听器会在我的缓存值过期后跟随,但似乎并非如此。

Hazelcast 是在从缓存中删除过期值之后调用 onRemoved 侦听器,还是仅在显式调用 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));

        }
    }
}

我的 Listener 相当简单:

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 调用,因为它会从缓存中删除过期值,但似乎从来没有。

这是预期的行为吗?我的缓存配置中是否缺少某些内容?

【问题讨论】:

    标签: caching hazelcast


    【解决方案1】:

    根据 JCache 规范第 8.4 节,REMOVED 事件仅用于显式操作。

    收听EXPIRED 事件会更好,但仍不理想。

    注意规范中的措辞和代码hereEXPIRED 事件依赖于实现——允许缓存提供者永远不会注意到数据已过期,永远不会删除它,因此永远不会生成事件。

    Hazelcast 确实注意到 see here,但这使得您需要的事件的及时出现取决于实现。

    【讨论】:

    • 所以我已经通读了该文档,确实它表明了这一点,但仅在 JCache 实现标题下。所以我不确定这是否仅在 Hazelcast 用作 JCache 实现时,或者即使它在没有 JCache API 的情况下本机使用。我似乎找不到任何文档或参考资料表明删除过期值是否会触发已删除的侦听器。
    • 您可以提出问题github.com/jsr107/jsr107spec/issues 来澄清规范?
    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2022-06-10
    • 2016-09-15
    • 1970-01-01
    相关资源
    最近更新 更多