gpt4 book ai didi

java - Hazelcast 近缓存无法使用简单示例

转载 作者:行者123 更新时间:2023-12-02 10:21:01 26 4
gpt4 key购买 nike

我目前使用的是 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 服务器端没有任何变化。

最佳答案

@塔特卡尔

  1. map.set 使附近的缓存无效,不要将新值放在那里
  2. 近端缓存仅用于基于键的访问,您的循环根本不会命中近端缓存。您需要像这样更改循环中的第二行: String value = closeCacheMap.get(entrySet.getKey()); 或将循环更改为 keySet,如
        for (Double key : nearCacheMap.keySet()) {
String value = entrySet.getValue(key);
}
  • 即使在更改之后,您仍然会看到 0,因为您只执行了 1 次获取操作,并且这是缓存未命中。如果您多次重复循环和统计打印,您将看到以下内容:
  • ---------------------------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

    关于java - Hazelcast 近缓存无法使用简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340342/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com