我目前正在使用 hazelcast 3.9版
我尝试了几种实现近缓存的方法,但似乎找不到正确的方法。下面我分享了我的代码,让我确切地知道我哪里出错了。
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));
}
}
检查 NearCache 统计信息时得到的输出完全为 0。
HazelcastConfig.java 文件
public class HazelcastConfig
{
public HazelcastInstance getHZInstance() throws IOException
{
ClientConfig cfg = new XmlClientConfigBuilder("src/main/resources/hazelcast-client.xml").build();
return HazelcastClient.newHazelcastClient(cfg);
}
}
Hazelcast 客户端的配置
<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"/>
我还尝试在 hazelcast-client.xml 文件中更改缓存名称。似乎没有任何效果
在 hazelcast 服务器端没有变化。