Java Hazelcast近缓存不使用简单示例

Java Hazelcast近缓存不使用简单示例,java,hazelcast,hazelcast-imap,near-cache,Java,Hazelcast,Hazelcast Imap,Near Cache,我目前正在使用hazelcast版本3.9 我尝试了几种实现近缓存的方法,但似乎找不到正确的方法。下面我分享了我的代码,让我知道我到底哪里出错了 public class NearCacheExample { public static void main(String[] args) throws IOException { HazelcastConfig hzConfig = new HazelcastConfig(); Haze

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

@Tatkal

  • map.set
    使近缓存放置无效不要将新值放在那里
  • 近缓存仅用于基于密钥的访问,循环根本不会命中近缓存。您需要像这样更改循环中的第二行:
    String value=nearCacheMap.get(entrySet.getKey())或将循环更改为键集,如
  • 即使在更改之后,您仍然会看到0,因为您只执行了1 get操作,并且这是缓存未命中。如果多次重复循环和统计打印,您将看到:

  • 我想你的意思是:
    String value=nearCacheMap.get(entrySet.getKey())或更简化:for(双键:nearCacheMap.keySet()){String value=nearCacheMap.get(key);}@OzanKılıç是的,没错,我把它改成了keySet,因为它是cheaper@GokhanOner感谢您的回复…我忘记接受您的回答。参考:,驱逐策略=
    :将不会驱逐任何项目,并且将忽略属性的最大大小。您仍然可以将其与生存时间秒和最大空闲秒相结合,从近缓存中逐出项目。
    <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"/>
    
            for (Double key : nearCacheMap.keySet()) {
                String value = entrySet.getValue(key);
            }
    
    ---------------------------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