7.7. 在嵌入式缓存中手动合并分区
检测并解决冲突的条目,以便在发生网络分区后手动合并嵌入缓存。
流程
从
EmbeddedCacheManager
检索ConflictManager
,以检测和解决缓存中冲突的条目,如下例所示:EmbeddedCacheManager manager = new DefaultCacheManager("example-config.xml"); Cache<Integer, String> cache = manager.getCache("testCache"); ConflictManager<Integer, String> crm = ConflictManagerFactory.get(cache.getAdvancedCache()); // Get all versions of a key Map<Address, InternalCacheValue<String>> versions = crm.getAllVersions(1); // Process conflicts stream and perform some operation on the cache Stream<Map<Address, CacheEntry<Integer, String>>> conflicts = crm.getConflicts(); conflicts.forEach(map -> { CacheEntry<Integer, String> entry = map.values().iterator().next(); Object conflictKey = entry.getKey(); cache.remove(conflictKey); }); // Detect and then resolve conflicts using the configured EntryMergePolicy crm.resolveConflicts(); // Detect and then resolve conflicts using the passed EntryMergePolicy instance crm.resolveConflicts((preferredEntry, otherEntries) -> preferredEntry);
注意
虽然 ConflictManager::getConflicts
流由每个条目处理,但底层的拆分器对每个网段上的缓存条目进行延迟。