Док здесь говорит об этом: < /p>
ближайший кэш на стороне клиента должен иметь то же имя, что и структура данных
на члене, для которого это рядом Кэш создается.
Вы можете использовать подстановочные знаки, поэтому в этом примере в основном читает* также соответствовать
Map в основном читайте. < /P>
< /blockquote>
Ясно, что рядом Конфигурация кэша живет на стороне клиента, а ID MAP используется для идентификации распределенной карты в кластере Hazelcast. Правильно, если имя конфигурации в ближнем ходе равна идентификатору MAP: < /p>
Код: Выделить всё
... new NearCacheConfig("counter")
... hazelcastClient.getMap("counter")
< /code>
Я попробовал следующие комбинации, но не работает из них, мой ближний конфиг не используется: < /p>
[list]
[*]new NearCacheConfig("counter)
[*]
Код: Выделить всё
new NearCacheConfig("counterId)
[/list]
Это мой тестовый код:
Код: Выделить всё
public class HazelcastConfiguration {
// THE FOLLOWING TWO VALUES MUST BE IDENTICAL
// e.g. "counter-config" and "counter" DOES NOT WORK
public static final String NEAR_CACHE_CONFIG_NAME = "counter";
public static final String COUNTER_MAP_ID = "counter";
private static final String HZ_CLUSTER_NAME = "gombi-dev";
private static final String HZ_CLUSTER_ADDRESS_1 = "localhost:13063";
private static final String HZ_CLUSTER_ADDRESS_2 = "localhost:13073";
public static HazelcastInstance getHazelcastInstance() {
NearCacheConfig nearCacheConfigForCounterMap = new NearCacheConfig(NEAR_CACHE_CONFIG_NAME)
.setInMemoryFormat(InMemoryFormat.BINARY)
.setTimeToLiveSeconds(30)
.setMaxIdleSeconds(10)
.setEvictionConfig(new EvictionConfig()
.setEvictionPolicy(EvictionPolicy.LRU)
.setSize(1000));
ClientConfig clientConfig = new ClientConfig();
clientConfig.setClusterName(HZ_CLUSTER_NAME);
clientConfig.getNetworkConfig().getAddresses().add(HZ_CLUSTER_ADDRESS_1);
clientConfig.getNetworkConfig().getAddresses().add(HZ_CLUSTER_ADDRESS_2);
clientConfig.addNearCacheConfig(nearCacheConfigForCounterMap);
return HazelcastClient.newHazelcastClient(clientConfig);
}
}
Код: Выделить всё
public class NearCacheReader {
public static void main(String[] args) {
String username = "zappee";
String key = MapKeyGenerator.getKey(username);
HazelcastInstance hazelcastClient = HazelcastConfiguration.getHazelcastInstance();
TimerTask task = new TimerTask() {
@Override
public void run() {
// nearCache will never be null
IMap nearCache = hazelcastClient.getMap(HazelcastConfiguration.COUNTER_MAP_ID);
Integer value = nearCache.get(key);
if (Objects.isNull(value)) {
System.out.printf(
"[%s] near-cache entry miss: {username: \"%s\"}%n",
LocalTimeConverter.nowAsString(),
username);
} else {
System.out.printf(
"[%s] near-cache entry hit: {username: \"%s\", value: %s}%n",
LocalTimeConverter.nowAsString(),
username,
value);
}
}
};
// Schedule the timer task to run after 1 second
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 1000);
}
}
Код: Выделить всё
public class NearCacheUpdater {
public static void main(String[] args) {
String username = "zappee";
String key = MapKeyGenerator.getKey(username);
HazelcastInstance hazelcastClient = HazelcastConfiguration.getHazelcastInstance();
IMap nearCache = hazelcastClient.getMap(HazelcastConfiguration.COUNTER_MAP_ID);
nearCache.put(key, 1);
TimerTask task = new TimerTask() {
@Override
public void run() {
int value = nearCache.get(key);
value++;
nearCache.put(key, value);
System.out.printf(
"[%s] value in the cache has been updated: {username: \"%s\", new-value: %s}\n",
LocalTimeConverter.nowAsString(),
username,
value);
}
};
// Schedule the timer task to run after 1 second
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 1000);
}
}
Код: Выделить всё
[23:27:32] near-cache entry miss: {username: "zappee"}
[23:27:33] near-cache entry miss: {username: "zappee"}
...
[23:27:57] near-cache entry miss: {username: "zappee"}
[23:27:58] near-cache entry hit: {username: "zappee", value: 12}
[23:27:59] near-cache entry hit: {username: "zappee", value: 12}
...
[23:28:07] near-cache entry hit: {username: "zappee", value: 12}
[23:28:08] near-cache entry hit: {username: "zappee", value: 22}
...
[23:28:17] near-cache entry hit: {username: "zappee", value: 22}
[23:28:18] near-cache entry hit: {username: "zappee", value: 32}
...
Код: Выделить всё
[23:27:47] value in the cache has been updated: {username: "zappee", new-value: 2}
[23:27:48] value in the cache has been updated: {username: "zappee", new-value: 3}
[23:27:49] value in the cache has been updated: {username: "zappee", new-value: 4}
...
[23:28:28] value in the cache has been updated: {username: "zappee", new-value: 43}
...
Код: Выделить всё
[23:37:01] near-cache entry miss: {username: "zappee"}
[23:37:02] near-cache entry miss: {username: "zappee"}
...
[23:37:09] near-cache entry miss: {username: "zappee"}
[23:37:10] near-cache entry hit: {username: "zappee", value: 2}
[23:37:11] near-cache entry hit: {username: "zappee", value: 3}
[23:37:12] near-cache entry hit: {username: "zappee", value: 4}
[23:37:13] near-cache entry hit: {username: "zappee", value: 5}
[23:37:14] near-cache entry hit: {username: "zappee", value: 6}
...
Что я здесь пропустил?
Подробнее здесь: https://stackoverflow.com/questions/794 ... ve-to-be-t