I'm currently using hazelcast version 3.9
I have tried several ways to implement Near cache and it seems I am not able to find the correct way. Below I have share my code and let me know exactly where I am going wrong.
public class NearCacheExample {
public static void main(String[] args) throws IOException
{
HazelcastConfig hzConfig = new HazelcastConfig();
HazelcastInstance hzInstance = hzConfig.getHZInstance();
IMap<Double, String> nearCacheMap = hzInstance.getMap("cacheExample");
for (int i = 0; i < 100000; i++) {
nearCacheMap.set(Math.random(), i + "");
}
long startTime = System.currentTimeMillis();
System.out.println("---------------------------Before Sort----------------------------------");
for (Entry<Double, String> entrySet : nearCacheMap.entrySet()) {
Double key = entrySet.getKey();
String value = entrySet.getValue();
}
long endTime = System.currentTimeMillis();
System.out.println("------------------------------------------------Read Both---------------------------------------------------");
NearCacheStats nearCacheStatistics = nearCacheMap.getLocalMapStats().getNearCacheStats();
System.out.println( "Near Cache hit/miss ratio 3= "
+ nearCacheStatistics.getHits());
System.out.println("Near cache implemented or not " + nearCacheMap.getLocalMapStats().getNearCacheStats().getOwnedEntryCount());
System.out.println(" EndTime timeDifference : " + startTime + " " + endTime + " " +(endTime-startTime));
}
}
The output I am getting while I check for NearCache stats is completely 0.
HazelcastConfig.java file
public class HazelcastConfig
{
public HazelcastInstance getHZInstance() throws IOException
{
ClientConfig cfg = new XmlClientConfigBuilder("src/main/resources/hazelcast-client.xml").build();
return HazelcastClient.newHazelcastClient(cfg);
}
}
Configuration at Hazelcast client
<near-cache name="default">
<in-memory-format>BINARY</in-memory-format>
<invalidate-on-change>true</invalidate-on-change>
<eviction eviction-policy="NONE" max-size-policy="ENTRY_COUNT" size="10"/>
I have also tried changing cache name inside hazelcast-client.xml file. Nothing seems to work
On the hazelcast server side there is no changes.
@Tatkal
map.set
invalidates the near cache put don't put the new value thereString value = nearCacheMap.get(entrySet.getKey());
or change the loop to keySet like for (Double key : nearCacheMap.keySet()) {
String value = entrySet.getValue(key);
}
---------------------------Before Sort----------------------------------
------------------------------------------------Read Both---------------------------------------------------
Near Cache hit/miss ratio = 0 / 100000
Near cache implemented or not 10
EndTime timeDifference : 1548313357643 1548313362527 4884
------------------------------------------------Read Both---------------------------------------------------
Near Cache hit/miss ratio = 10 / 199990
Near cache implemented or not 10
EndTime timeDifference : 1548313357643 1548313367155 9512
------------------------------------------------Read Both---------------------------------------------------
Near Cache hit/miss ratio = 20 / 299980
Near cache implemented or not 10
EndTime timeDifference : 1548313357643 1548313371688 14045