Programmatically configuring Near Cache in hazelcast and Java. Make sure if Near cache returns data from local cache
I created a Hazelcast configured with one node running a Hazelcast instance. My client app uses client config to read cache from Hazelcast instance. I want to implement near cache in my client application so I can use it as local cache. Could you please provide me with an example where I can see how it is used in Java. My current code is
Hazel Cast Cache Node
public class HazelCastNode1 {
public static void main(String[] args) {
Config cfg = new Config();
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
Map<Integer, String> citiesMap = instance.getMap("Cities");
for(int i = 0; i < 100000; i++){
citiesMap.put(i, "Vienna"+i);
}
System.out.println("Map Size:" + citiesMap.size());
}
}
Client code
public class ReadClient {
public static void main(String[] args) {
ClientConfig clientConfig = new ClientConfig();
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap<Integer, String> cumap = client.getMap("Cities");
System.out.println(cumap.size());
}
}
Could you please give me an example that I could bring up here for a near cache implementation.
+3
viren
source
to share
2 answers
Hi i can understand this code
public class ReadClient {
public static void main(String[] args) {
ClientConfig clientConfig = new ClientConfig();
NearCacheConfig ncc = clientConfig.getNearCacheConfig("Cities");
if(ncc == null){
ncc = new NearCacheConfig();
}
ncc.setCacheLocalEntries(true);
ncc.setEvictionPolicy("LRU");
ncc.setMaxSize(500000);
ncc.setInvalidateOnChange(true);
Map<String, NearCacheConfig> nearCache = new HashMap<String, NearCacheConfig>();
nearCache.put("CitiesLocal", ncc);
clientConfig.addNearCacheConfig("Cities", ncc);
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap<Integer, String> cumap = client.getMap("Cities");
System.out.println(cumap.size());
for(int i = 0 ; i < 100; i++){
cumap.get(1);
}
System.out.println(cumap.getLocalMapStats().getNearCacheStats().getHits());
cumap = client.getMap("CitiesLocal");
System.out.println(cumap.size());
}
}
But I still have one problem, how to check that the near cache is returning data and this is not a remote call every time.
+4
viren
source
to share
I don't know if there is registration if hit near cache.
To read the metrics:
yourmap.getLocalMapStats().getNearCacheStats().getHits()
0
pveentjer
source
to share