Anyway to view Cache entry metadata from Hazelcast (ie: date added, last accessed, etc)?

538 Views Asked by At

I'm using Hazelcast with a distributed cache in embedded mode. My cache is defined with an Eviction Policy and an Expiry Policy.

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);

The problem I am having is that my Listener seems to be firing prematurely in a production environment; the listener is executed (REMOVED) even though the cache has been recently queried.

As the expiry listener fires, I get the CacheEntry, but I'd like to be able to see the reason for the Expiry, whether it was Evicted (due to MaxSize policy), or Expired (Duration). If Expired, I'd like to see the timestamp of when it was last accessed. If Evicted, I'd like to see the number of entries in the cache, etc.

Are these stats/metrics/metadata available anywhere via Hazelcast APIs?

1

There are 1 best solutions below

1
Vassilis Bekiaris On

Local cache statistics (entry count, eviction count) are available using ICache#getLocalCacheStatistics(). Notice that you need to setStatisticsEnabled(true) in your cache configuration for statistics to be available. Also, notice that the returned CacheStatistics object only reports statistics for the local member.

When seeking info on a single cache entry, you can use the EntryProcessor functionality to unwrap the MutableEntry to the Hazelcast-specific class com.hazelcast.cache.impl.CacheEntryProcessorEntry and inspect that one. The Hazelcast-specific implementation provides access to the CacheRecord that provides metadata like creation/accessed time.

Caveat: the Hazelcast-specific implementation may change between versions. Here is an example:

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;
        });