1

我在嵌入式模式下使用带有分布式缓存的 Hazelcast。我的缓存是用驱逐策略和过期策略定义的。

CacheSimpleConfig cacheSimpleConfig = new CacheSimpleConfig()
        .setName(CACHE_NAME)
        .setKeyType(UserRolesCacheKey.class.getName())
        .setValueType((new String[0]).getClass().getName())
        .setStatisticsEnabled(false)
        .setManagementEnabled(false)
        .setReadThrough(true)
        .setWriteThrough(true)
        .setInMemoryFormat(InMemoryFormat.OBJECT)
        .setBackupCount(1)
        .setAsyncBackupCount(1)
        .setEvictionConfig(new EvictionConfig()
                .setEvictionPolicy(EvictionPolicy.LRU)
                .setSize(1000)
                .setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT))
        .setExpiryPolicyFactoryConfig(
                new ExpiryPolicyFactoryConfig(
                        new TimedExpiryPolicyFactoryConfig(ACCESSED,
                                new DurationConfig(
                                        1800,
                                        TimeUnit.SECONDS))));

hazelcastInstance.getConfig().addCacheConfig(cacheSimpleConfig);

ICache<UserRolesCacheKey, String[]> userRolesCache = hazelcastInstance.getCacheManager().getCache(CACHE_NAME);
MutableCacheEntryListenerConfiguration<UserRolesCacheKey, String[]> listenerConfiguration =
        new MutableCacheEntryListenerConfiguration<>(
                new UserRolesCacheListenerFactory(), null, false, false);
userRolesCache.registerCacheEntryListener(listenerConfiguration);

我遇到的问题是我的侦听器似乎在生产环境中过早触发;即使最近查询了缓存,侦听器也会被执行(删除)。

当过期监听器触发时,我得到了 CacheEntry,但我希望能够看到过期的原因,它是被驱逐(由于 MaxSize 策略)还是过期(持续时间)。如果已过期,我想查看上次访问时间的时间戳。如果被驱逐,我想查看缓存中的条目数等。

这些统计/指标/元数据是否可以通过 Hazelcast API 在任何地方获得?

4

1 回答 1

1

本地缓存统计信息(条目计数、逐出计数)可使用ICache#getLocalCacheStatistics(). 请注意,您需要setStatisticsEnabled(true)在缓存配置中才能使用统计信息。此外,请注意返回的CacheStatistics对象仅报告本地成员的统计信息。

在查找有关单个缓存条目的信息时,您可以使用该EntryProcessor功能将其解MutableEntry包到特定于 Hazelcast 的类com.hazelcast.cache.impl.CacheEntryProcessorEntry并检查该类。Hazelcast 特定的实现提供对CacheRecord提供元数据(如创建/访问时间)的访问。

警告:特定于 Hazelcast 的实现可能会在版本之间发生变化。这是一个例子:

cache.invoke(KEY, (EntryProcessor<String, String, Void>) (entry, arguments) -> {
            CacheEntryProcessorEntry hzEntry = entry.unwrap(CacheEntryProcessorEntry.class);
            // getRecord does not update the entry's access time
            System.out.println(hzEntry.getRecord().getLastAccessTime());
            return null;
        });
于 2019-08-13T10:08:46.653 回答