1

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?

Eric B.
  • 23,425
  • 50
  • 169
  • 316
  • 1
    You are addressing at least 5 interesting topics with one question. Can you split that, please? If you like to solve a specific problem with your construction, state the problem, or, if you like to know specifics of one product and API, then separate the questions into each product and specific API item. – cruftex Aug 13 '19 at 09:05
  • You are talking about JCache expiry listeners, but I cannot see any JCache. Hint: In JCache lingo expiry means always the duration elapsed. – cruftex Aug 13 '19 at 09:06
  • @cruftex You're right - I thought I had been using the JCache listeners, but I guess I ended up using the ICache listeners instead. Thanks for pointing that out. I've cleaned up the question a bit. – Eric B. Aug 13 '19 at 15:36

1 Answers1

1

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;
        });
  • Thanks, but if I want to dump the contents of every record in the cache, without knowing the keys, how can I iterate over its contents, or retrieve a list of the keys without updating the LastAccess time? Is it even feasible? I can't seem to find any methods in ICache or Cache which would accomplish that. – Eric B. Aug 15 '19 at 18:08