@Test public void testNearCache() throws Exception { final Config config = new Config(); NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setMaxSize(1000); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); IMap m1 = h1.getMap("default"); m1.put("1", "value"); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); IMap m2 = h2.getMap("default"); assertEquals("value", m2.get("1")); assertEquals("value", m1.get("1")); HazelcastClient client1 = newHazelcastClient(h1); HazelcastClient client2 = newHazelcastClient(h2); for (int i = 0; i < 10; i++) { assertEquals("value", client1.getMap("default").get("1")); assertEquals("value", client2.getMap("default").get("1")); } }
public GuavaNearCacheImpl(NearCacheConfig nc, final MapClientProxy<K,V> map) { this.map = map; CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(nc.getMaxSize()); if (nc.getTimeToLiveSeconds() > 0) cacheBuilder.expireAfterWrite(nc.getTimeToLiveSeconds(), TimeUnit.SECONDS); if (nc.getMaxIdleSeconds() > 0) cacheBuilder.expireAfterAccess(nc.getMaxIdleSeconds(), TimeUnit.SECONDS); cache = cacheBuilder.build(new CacheLoader() { @Override public Object load(Object o) throws Exception { try { return map.get0(o); } catch (Exception e) { throw new ExecutionException(e); } } }); }
@Test public void testReadFromCache() { System.setProperty("hazelcast.client.near.cache.enabled", "true"); Config config = new Config(); NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setMaxSize(1000); nearCacheConfig.setMaxIdleSeconds(10); nearCacheConfig.setTimeToLiveSeconds(20); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); HazelcastInstance h = Hazelcast.newHazelcastInstance(config); String mapName = "testReadFromCache"; IMap<String, String> mapH = h.getMap(mapName); mapH.put("a", "a"); HazelcastClient client = HazelcastClient.newHazelcastClient(new ClientConfig()); Map<Object, Object> mapC = client.getMap(mapName); assertEquals("a", mapC.get("a")); int hit = mapH.getMapEntry("a").getHits(); mapC.get("a"); assertEquals(mapH.getMapEntry("a").getHits(), hit); for (int i = 0; i < 100; i++) { mapC.get("a"); } assertEquals(mapH.getMapEntry("a").getHits(), hit); }
@Test public void testNoNearCacheConfig() { System.setProperty("hazelcast.client.near.cache.enabled", "false"); Config config = new Config(); NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setMaxSize(1000); nearCacheConfig.setMaxIdleSeconds(10); nearCacheConfig.setTimeToLiveSeconds(20); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); HazelcastInstance h = Hazelcast.newHazelcastInstance(config); String mapName = "testReadFromCache"; IMap<String, String> mapH = h.getMap(mapName); mapH.put("a", "a"); HazelcastClient client = HazelcastClient.newHazelcastClient(new ClientConfig()); Map<Object, Object> mapC = client.getMap(mapName); assertEquals("a", mapC.get("a")); int hit = mapH.getMapEntry("a").getHits(); mapC.get("a"); assertEquals(mapH.getMapEntry("a").getHits(), ++hit); for (int i = 0; i < 100; i++) { mapC.get("a"); } assertEquals(mapH.getMapEntry("a").getHits(), hit + 100); }
@Test public void testInvalidateOnChange() throws InterruptedException { System.setProperty("hazelcast.client.near.cache.enabled", "true"); Config config = new Config(); NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setMaxSize(1000); nearCacheConfig.setInvalidateOnChange(true); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); HazelcastInstance h = Hazelcast.newHazelcastInstance(config); String mapName = "testInvalidateOnChange"; IMap<String, String> mapH = h.getMap(mapName); mapH.put("a", "a"); HazelcastClient client = HazelcastClient.newHazelcastClient(new ClientConfig()); Map<Object, Object> mapC = client.getMap(mapName); int hit = mapH.getMapEntry("a").getHits(); assertEquals("a", mapC.get("a")); assertEquals(mapH.getMapEntry("a").getHits(), ++hit); mapC.get("a"); assertEquals(mapH.getMapEntry("a").getHits(), hit); mapH.put("a", "b"); Thread.sleep(100); assertEquals("b", mapC.get("a")); assertEquals(mapH.getMapEntry("a").getHits(), ++hit); }
public static void setupConfig(HazelcastConnection hazelcastConnection) { MapConfig mapConfig = hazelcastConnection.getHazelcastConfig().getMapConfig(NAME_PREFIX + "*"); mapConfig.setInMemoryFormat(InMemoryFormat.BINARY); mapConfig.setTimeToLiveSeconds(10); mapConfig.setMaxIdleSeconds(10); mapConfig.setBackupCount(0); NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setTimeToLiveSeconds(10); nearCacheConfig.setInvalidateOnChange(true); nearCacheConfig.setMaxIdleSeconds(10); mapConfig.setNearCacheConfig(nearCacheConfig); }
@Test public void testIssue174NearCacheContainsKeySingleNode() { Config config = new Config(); config.getGroupConfig().setName("testIssue174NearCacheContainsKeySingleNode"); NearCacheConfig nearCacheConfig = new NearCacheConfig(); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); HazelcastInstance h = Hazelcast.newHazelcastInstance(config); IMap<String, String> map = h.getMap("testIssue174NearCacheContainsKeySingleNode"); map.put("key", "value"); assertTrue(map.containsKey("key")); h.getLifecycleService().shutdown(); }