public NearCacheConfig asNearCacheConfig(SerializationService serializationService) { NearCacheConfig config = new NearCacheConfig(); config.setName(name); config.setInMemoryFormat(inMemoryFormat); config.setSerializeKeys(serializeKeys); config.setInvalidateOnChange(invalidateOnChange); config.setTimeToLiveSeconds(timeToLiveSeconds); config.setMaxIdleSeconds(maxIdleSeconds); config.setEvictionConfig(evictionConfigHolder.asEvictionConfg(serializationService)); config.setCacheLocalEntries(cacheLocalEntries); config.setLocalUpdatePolicy(LocalUpdatePolicy.valueOf(localUpdatePolicy)); config.setPreloaderConfig(preloaderConfig); return config; }
ncc = new NearCacheConfig(); ncc.setCacheLocalEntries(true); ncc.setEvictionPolicy("LRU"); ncc.setMaxSize(500000); ncc.setInvalidateOnChange(true); Map<String, NearCacheConfig> nearCache = new HashMap<String, NearCacheConfig>(); nearCache.put("CitiesLocal", ncc);
public static NearCacheConfigHolder of(NearCacheConfig config, SerializationService serializationService) { if (config == null) { return null; } return new NearCacheConfigHolder(config.getName(), config.getInMemoryFormat().name(), config.isSerializeKeys(), config.isInvalidateOnChange(), config.getTimeToLiveSeconds(), config.getMaxIdleSeconds(), EvictionConfigHolder.of(config.getEvictionConfig(), serializationService), config.isCacheLocalEntries(), config.getLocalUpdatePolicy().name(), config.getPreloaderConfig()); } }
protected NearCacheConfig createNearCacheConfig(String cacheName, InMemoryFormat inMemoryFormat) { return new NearCacheConfig() .setName(DEFAULT_CACHE_NAME) .setInMemoryFormat(inMemoryFormat); }
protected AbstractNearCacheRecordStore(NearCacheConfig nearCacheConfig, NearCacheStatsImpl nearCacheStats, SerializationService serializationService, ClassLoader classLoader) { this.nearCacheConfig = nearCacheConfig; this.inMemoryFormat = nearCacheConfig.getInMemoryFormat(); this.timeToLiveMillis = nearCacheConfig.getTimeToLiveSeconds() * MILLI_SECONDS_IN_A_SECOND; this.maxIdleMillis = nearCacheConfig.getMaxIdleSeconds() * MILLI_SECONDS_IN_A_SECOND; this.serializationService = serializationService; this.classLoader = classLoader; this.nearCacheStats = nearCacheStats; this.evictionDisabled = nearCacheConfig.getEvictionConfig().getEvictionPolicy() == EvictionPolicy.NONE; }
@SuppressWarnings("deprecation") private NearCacheConfig handleNearCacheConfig(Node node) { String name = getAttribute(node, "name"); NearCacheConfig nearCacheConfig = new NearCacheConfig(name); Boolean serializeKeys = null; for (Node child : childElements(node)) { String value = getTextContent(child).trim(); if ("max-size".equals(nodeName)) { nearCacheConfig.setMaxSize(Integer.parseInt(value)); LOGGER.warning("The element <max-size/> for <near-cache/> is deprecated, please use <eviction/> instead!"); } else if ("time-to-live-seconds".equals(nodeName)) { nearCacheConfig.setTimeToLiveSeconds(Integer.parseInt(value)); } else if ("max-idle-seconds".equals(nodeName)) { nearCacheConfig.setMaxIdleSeconds(Integer.parseInt(value)); } else if ("eviction-policy".equals(nodeName)) { nearCacheConfig.setEvictionPolicy(value); LOGGER.warning("The element <eviction-policy/> for <near-cache/> is deprecated, please use <eviction/> instead!"); } else if ("in-memory-format".equals(nodeName)) { nearCacheConfig.setInMemoryFormat(InMemoryFormat.valueOf(upperCaseInternal(value))); } else if ("serialize-keys".equals(nodeName)) { serializeKeys = Boolean.parseBoolean(value); nearCacheConfig.setSerializeKeys(serializeKeys); } else if ("invalidate-on-change".equals(nodeName)) { nearCacheConfig.setInvalidateOnChange(Boolean.parseBoolean(value)); } else if ("cache-local-entries".equals(nodeName)) { nearCacheConfig.setCacheLocalEntries(Boolean.parseBoolean(value)); } else if ("local-update-policy".equals(nodeName)) { NearCacheConfig.LocalUpdatePolicy policy = NearCacheConfig.LocalUpdatePolicy.valueOf(value); nearCacheConfig.setLocalUpdatePolicy(policy);
NearCacheConfig cacheMapNLConfig = new NearCacheConfig(); cacheMapNLConfig.setCacheLocalEntries(false); cacheMapNLConfig.setEvictionPolicy("LRU"); cacheMapNLConfig.setInMemoryFormat(InMemoryFormat.OBJECT); cacheMapNLConfig.setInvalidateOnChange(true); cacheMapNLConfig.setMaxIdleSeconds(10 * 60); /* 10 minute idle time */ cacheMapNLConfig.setTimeToLiveSeconds(1 * 60 * 60); /* 1 hour TTL */ cacheMapConfig.setNearCacheConfig(cacheMapNLConfig); config.addMapConfig(cacheMapConfig);
public void run() { NearCacheConfig nearCacheConfig = createNearCacheConfig() .setInMemoryFormat(InMemoryFormat.OBJECT) .setCacheLocalEntries(true) .setInvalidateOnChange(false) .setEvictionConfig(createEvictionConfigWithEntryCountPolicy(RECORD_COUNT * 2)); ICache<Integer, Article> cache = createCacheWithNearCache(nearCacheConfig); Article article = new Article("foo"); cache.put(1, article); // the first get() will populate the Near Cache Article firstGet = cache.get(1); // the second and third get() will be served from the Near Cache Article secondGet = cache.get(1); Article thirdGet = cache.get(1); printNearCacheStats(cache); System.out.println("Since we use in-memory format BINARY, the article instances from the Near Cache will be different."); System.out.println("Compare first and second article instance: " + (firstGet == secondGet)); System.out.println("Compare second and third article instance: " + (secondGet == thirdGet)); HazelcastClient.shutdownAll(); Hazelcast.shutdownAll(); }
private static ClientConfig newClientConfig(String mapName) { NativeMemoryConfig memoryConfig = new NativeMemoryConfig(); memoryConfig.setEnabled(true); memoryConfig.setSize(new MemorySize(128, MEGABYTES)); memoryConfig.setAllocatorType(STANDARD); NearCacheConfig nearCacheConfig = new NearCacheConfig(); EvictionConfig evictionConfig = nearCacheConfig.getEvictionConfig(); evictionConfig.setMaximumSizePolicy(USED_NATIVE_MEMORY_PERCENTAGE); evictionConfig.setSize(90); nearCacheConfig.setInMemoryFormat(NATIVE); nearCacheConfig.setInvalidateOnChange(true); nearCacheConfig.setName(mapName); ClientConfig clientConfig = new ClientConfig(); clientConfig.setNativeMemoryConfig(memoryConfig); clientConfig.addNearCacheConfig(nearCacheConfig); clientConfig.setLicenseKey(ENTERPRISE_LICENSE_KEY); return clientConfig; } }
@SuppressWarnings("deprecation") private static void mapNearCacheConfigXmlGenerator(XmlGenerator gen, NearCacheConfig n) { if (n != null) { if (n.getName() != null) { gen.open("near-cache", "name", n.getName()); } else { gen.open("near-cache"); } gen.node("in-memory-format", n.getInMemoryFormat()) .node("invalidate-on-change", n.isInvalidateOnChange()) .node("time-to-live-seconds", n.getTimeToLiveSeconds()) .node("max-idle-seconds", n.getMaxIdleSeconds()) .node("serialize-keys", n.isSerializeKeys()) .node("cache-local-entries", n.isCacheLocalEntries()) .node("max-size", n.getMaxSize()) .node("eviction-policy", n.getEvictionPolicy()); evictionConfigXmlGenerator(gen, n.getEvictionConfig()); gen.close(); } }
public void run() { NearCacheConfig nearCacheConfig1 = createNearCacheConfig() .setInMemoryFormat(InMemoryFormat.OBJECT) .setCacheLocalEntries(true) .setInvalidateOnChange(false) .setEvictionConfig(createEvictionConfigWithEntryCountPolicy(RECORD_COUNT * 2)); .setInMemoryFormat(InMemoryFormat.OBJECT) .setSerializeKeys(true) .setCacheLocalEntries(true) .setInvalidateOnChange(false) .setEvictionConfig(createEvictionConfigWithEntryCountPolicy(RECORD_COUNT * 2)); System.out.println("Near Cache of cache 1 uses serialized keys: " + nearCacheConfig1.isSerializeKeys()); System.out.println("Near Cache of cache 2 uses serialized keys: " + nearCacheConfig2.isSerializeKeys());
/** * This method creates a copy of {@link NearCacheConfig} <strong>without</strong> copying read-only objects for * class fields, which may contain trash from previous invocations of near cache methods (this problem comes from * the fact, that HZ treats map configs as a const values after cluster startup, but atlassian cache allows to * change them after cluster is stared) * * @param nearCacheConfig {@link NearCacheConfig} to be copied * @return copy of the {@link NearCacheConfig} without read-only objects */ private NearCacheConfig copyNearCacheConfig(@Nonnull NearCacheConfig nearCacheConfig) { NearCacheConfig nearCacheConfigCopy = new NearCacheConfig(nearCacheConfig); EvictionConfig evictionConfigCopy = new EvictionConfig(nearCacheConfig.getEvictionConfig()); nearCacheConfigCopy.setEvictionConfig(evictionConfigCopy); NearCachePreloaderConfig preloaderConfigCopy = new NearCachePreloaderConfig(nearCacheConfigCopy.getPreloaderConfig()); nearCacheConfigCopy.setPreloaderConfig(preloaderConfigCopy); return nearCacheConfigCopy; }
new NearCacheConfig() : copyNearCacheConfig(mapConfig.getNearCacheConfig()); nearCacheConfig.setMaxSize(maxSize); nearCacheConfig.setEvictionPolicy(EvictionPolicy.LFU.name()); nearCacheConfig.setTimeToLiveSeconds(Math.max(1, nearCacheTtl)); nearCacheConfig.setTimeToLiveSeconds(timeToLiveSeconds);
public void run() { NearCacheConfig nearCacheConfig = createNearCacheConfig() .setInMemoryFormat(InMemoryFormat.OBJECT) .setInvalidateOnChange(true) .setEvictionConfig(createEvictionConfigWithEntryCountPolicy(RECORD_COUNT * 2)); ICache<String, Article> cache1 = createCacheWithNearCache(nearCacheConfig); ICache<String, Article> cache2 = createCacheWithNearCache(nearCacheConfig); String key = generateKeyOwnedBy(getServerInstance()); cache2.put(key, new Article("foo")); printNearCacheStats(cache1, "The cache2.put(key, new Article(\"foo\")) call has no effect on the Near Cache of cache1"); cache1.get(key); printNearCacheStats(cache1, "The first cache1.get(key) call populates the Near Cache of cache1"); cache2.put(key, new Article("bar")); printNearCacheStats(cache1, "The cache2.put(key, new Article(\"bar\") call will invalidate the Near Cache on cache1"); waitForInvalidationEvents(); printNearCacheStats(cache1, "The Near Cache of cache1 is empty after the invalidation event has been processed"); cache1.get(key); printNearCacheStats(cache1, "The next cache1.get(key) call populates the Near Cache again"); HazelcastClient.shutdownAll(); Hazelcast.shutdownAll(); }
public void run() { NearCacheConfig nearCacheConfig = createNearCacheConfig() .setInMemoryFormat(InMemoryFormat.OBJECT) .setInvalidateOnChange(false) .setLocalUpdatePolicy(NearCacheConfig.LocalUpdatePolicy.CACHE) .setEvictionConfig(createEvictionConfigWithEntryCountPolicy(RECORD_COUNT * 2)); ICache<String, Article> cache1 = createCacheWithNearCache(nearCacheConfig); ICache<String, Article> cache2 = createCacheWithNearCache(nearCacheConfig); String key = generateKeyOwnedBy(getServerInstance()); cache1.put(key, new Article("foo")); printNearCacheStats(cache1, "The cache1.put(key, new Article(\"foo\")) call will populate the Near Cache of cache1, ..."); printNearCacheStats(cache2, "..., but has no effect on the Near Cache of cache2"); cache1.get(key); printNearCacheStats(cache1, "The first cache1.get(key) call be served by the Near Cache of cache1"); cache2.get(key); printNearCacheStats(cache2, "The first cache2.get(key) call populates the Near Cache of cache2"); cache1.put(key, new Article("bar")); printNearCacheStats(cache1, "The cache1.put(key, new Article(\"bar\") call will update the Near Cache of cache1, ..."); printNearCacheStats(cache2, "..., but has no effect on the Near Cache of cache2"); Article article1 = cache1.get(key); printNearCacheStats(cache1, "The second cache1.get(key) call will be served by the Near Cache of cache1"); Article article2 = cache2.get(key); printNearCacheStats(cache2, "The second cache2.get(key) call will be served by the Near Cache of cache2"); System.out.printf("The retrieved articles are not the same: %s vs. %s%n", article1.getName(), article2.getName()); HazelcastClient.shutdownAll(); Hazelcast.shutdownAll(); }
public IdentifiedDataSerializable createNew(Integer arg) { return new NearCacheConfig(); } };
/** * Checks preconditions to create a map proxy with Near Cache. * * @param mapName name of the map that Near Cache will be created for * @param nearCacheConfig the {@link NearCacheConfig} to be checked * @param nativeMemoryConfig the {@link NativeMemoryConfig} of the Hazelcast instance * @param isClient {@code true} if the config is for a Hazelcast client, {@code false} otherwise */ public static void checkNearCacheConfig(String mapName, NearCacheConfig nearCacheConfig, NativeMemoryConfig nativeMemoryConfig, boolean isClient) { checkNotNativeWhenOpenSource(nearCacheConfig.getInMemoryFormat()); checkLocalUpdatePolicy(mapName, nearCacheConfig.getLocalUpdatePolicy()); checkEvictionConfig(nearCacheConfig.getEvictionConfig(), true); checkOnHeapNearCacheMaxSizePolicy(nearCacheConfig); checkNearCacheNativeMemoryConfig(nearCacheConfig.getInMemoryFormat(), nativeMemoryConfig, getBuildInfo().isEnterprise()); if (isClient && nearCacheConfig.isCacheLocalEntries()) { throw new IllegalArgumentException("The Near Cache option `cache-local-entries` is not supported in " + "client configurations."); } checkPreloaderConfig(nearCacheConfig, isClient); }
public static void initDefaultMaxSizeForOnHeapMaps(NearCacheConfig nearCacheConfig) { if (nearCacheConfig == null) { return; } EvictionConfig evictionConfig = nearCacheConfig.getEvictionConfig(); if (nearCacheConfig.getInMemoryFormat() != InMemoryFormat.NATIVE && !evictionConfig.sizeConfigured) { evictionConfig.setSize(EvictionConfig.DEFAULT_MAX_ENTRY_COUNT_FOR_ON_HEAP_MAP); } } }
@Override protected void onInitialize() { super.onInitialize(); logger = getContext().getLoggingService().getLogger(getClass()); NearCacheConfig nearCacheConfig = getContext().getClientConfig().getNearCacheConfig(name); serializeKeys = nearCacheConfig.isSerializeKeys(); NearCacheManager nearCacheManager = getContext().getNearCacheManager(); IMapDataStructureAdapter<K, V> adapter = new IMapDataStructureAdapter<K, V>(this); nearCache = nearCacheManager.getOrCreateNearCache(name, nearCacheConfig, adapter); if (nearCacheConfig.isInvalidateOnChange()) { registerInvalidationListener(); } }
Config config = new Config(); .... MapConfig mapCfg = new MapConfig(); mapCfg.setName("default"); mapCfg.setBackupCount(2); mapCfg.getMaxSizeConfig().setSize(10000); mapCfg.setTimeToLiveSeconds(300); MapStoreConfig mapStoreCfg = new MapStoreConfig(); mapStoreCfg.setClassName(MyMapStore.class.getName()).setEnabled(true); mapCfg.setMapStoreConfig(mapStoreCfg); // use near cache if needed NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setMaxSize(1000).setMaxIdleSeconds(120).setTimeToLiveSeconds(300); mapCfg.setNearCacheConfig(nearCacheConfig); config.addMapConfig(mapCfg); Hazelcast.newHazelcastInstance(config);