1

I am trying to build an example in order to check if I can access to cache in the second request.

In the first request I create the cache and I would like to check that the cache I available in the second request.

boolean existCache = false;
HazelcastInstance hz = null;
hz = Hazelcast.getHazelcastInstanceByName("hzInstanceTest");
if(hz == null)
{
    Config cfg = new Config();
    cfg.setInstanceName("hzInstanceTest");
    hz = Hazelcast.newHazelcastInstance(cfg);
}
Collection<DistributedObject> distributedObjects = hz.getDistributedObjects();

for (DistributedObject distributedObject : distributedObjects) {
    if ((distributedObject instanceof ICache) && (distributedObject.getName().equalsIgnoreCase("hzCacheTest"))) {
       System.out.println(distributedObject.getName());
       existCache = true;
       break;
    }
}
if (existCache)
{
    ICache<String, CaLpgDataCollectionDto<CaBigNumber>> icache  =  hz.getCacheManager().getCache("hzCacheTest");
    CaLpgDataCollectionDto<CaBigNumber> hzTestValue =  icache.get("hzCacheTest");
}
else
{
    CacheManager manager = Caching.getCachingProvider().getCacheManager();
    MutableConfiguration<String, CaLpgDataCollectionDto<CaBigNumber>> configuration = new MutableConfiguration<String, CaLpgDataCollectionDto<CaBigNumber>>();
    configuration.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES));
    Cache<String, CaLpgDataCollectionDto<CaBigNumber>> jcCacheTest = manager.createCache("jcCacheTest", configuration);
    //ICache is a Hazelcast interface that extends JCache, provides more functionality
    ICache<String, CaLpgDataCollectionDto<CaBigNumber>> hcCache = jcCacheTest.unwrap(ICache.class);
    final ExpiryPolicy customExpiryPolicy = AccessedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES).create();
    hcCache.put("hzCacheTest", lpgDatasource, customExpiryPolicy);
}    

I would like to do the following:

hcCache.put("hzCacheTest", lpgDatasource, customExpiryPolicy);

in the Hazelcast instad created. Kind regards.

Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
Hossain
  • 31
  • 6

0 Answers0