在hazelcast和Java中以编程方式设置Near Cache验证Near缓存是否从本地缓存返回数据
作者:互联网
我创建了一个Hazelcast设置,其中一个节点运行了hazelcast实例.我的客户端应用程序正在使用客户端配置从Hazelcast实例读取缓存.
我想在客户端应用程序中实现Near缓存,以将其用作本地缓存.
您能否提供一个示例,让我看到在Java中如何使用它.
我当前的代码是这样
榛树演员缓存节点
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());
}
}
客户代码
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());
}
}
您能否举个例子,在这里我可以适合实施“近缓存”.
解决方法:
您好能够弄清楚这是代码
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());
}
}
但是我仍然有一个问题,如何验证近缓存正在返回数据,而不是每次都不是远程调用.
标签:distributed-caching,hazelcast,java 来源: https://codeday.me/bug/20191029/1956926.html