为了账号安全,请及时绑定邮箱和手机立即绑定

Hazelcast Near Cache 不适用于简单示例

Hazelcast Near Cache 不适用于简单示例

当年话下 2022-05-21 20:28:13
我目前正在使用 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));    }}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 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

  1. map.set使附近的缓存无效,不要将新值放在那里

  2. Near Cache 仅用于基于键的访问,您的循环根本不会命中 Near Cache。您需要像这样更改循环中的第二行:String value = nearCacheMap.get(entrySet.getKey());或将循环更改为 keySet

        for (Double key : nearCacheMap.keySet()) {
                    String value = entrySet.getValue(key);
        }
  1. 即使更改后,您仍然会看到 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


查看完整回答
反对 回复 2022-05-21
  • 1 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信