Hazelcast Near Cache无法与简单示例一起使用

时间:2019-01-24 06:10:02

标签: java hazelcast hazelcast-imap near-cache

我当前使用的是hazelcast版本 3.9

我尝试了几种方法来实现Near缓存,但似乎找不到正确的方法。在下面,我分享了我的代码,并让我确切知道我要去哪里。

    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 个答案:

答案 0 :(得分:1)

@Tatkal

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