第 12 章 Multimap 缓存


MutimapCache 是一种 Data Grid Cache 类型,它将键映射到每个键可以包含多个值的值。

12.1. Multimap Cache

MutimapCache 是一种 Data Grid Cache 类型,它将键映射到每个键可以包含多个值的值。

12.1.1. 安装和配置

pom.xml

<dependency>
  <groupId>org.infinispan</groupId>
  <artifactId>infinispan-multimap</artifactId>
</dependency>

12.1.2. MultimapCache API

MultimapCache API 会公开几个与 Multimap Cache 交互的方法。在大多数情况下,这些方法是非阻塞的;如需更多信息,请参阅 限制。

public interface MultimapCache<K, V> {

   CompletableFuture<Optional<CacheEntry<K, Collection<V>>>> getEntry(K key);

   CompletableFuture<Void> remove(SerializablePredicate<? super V> p);

   CompletableFuture<Void> put(K key, V value);

   CompletableFuture<Collection<V>> get(K key);

   CompletableFuture<Boolean> remove(K key);

   CompletableFuture<Boolean> remove(K key, V value);

   CompletableFuture<Void> remove(Predicate<? super V> p);

   CompletableFuture<Boolean> containsKey(K key);

   CompletableFuture<Boolean> containsValue(V value);

   CompletableFuture<Boolean> containsEntry(K key, V value);

   CompletableFuture<Long> size();

   boolean supportsDuplicates();

}

CompletableFuture<Void> put (K key, V value)

将键值对放在多映射缓存中。

MultimapCache<String, String> multimapCache = ...;

multimapCache.put("girlNames", "marie")
             .thenCompose(r1 -> multimapCache.put("girlNames", "oihana"))
             .thenCompose(r3 -> multimapCache.get("girlNames"))
             .thenAccept(names -> {
                          if(names.contains("marie"))
                              System.out.println("Marie is a girl name");

                           if(names.contains("oihana"))
                              System.out.println("Oihana is a girl name");
                        });

这个代码的输出如下:

Marie is a girl name
Oihana is a girl name

CompletableFuture<Collection<V>> get (K key)

异步返回与此多映射缓存中与键关联的值(若有)的视图集合。对检索到的集合的任何更改都不会更改此多映射缓存中的值。当此方法返回空集合时,这意味着未找到键。

CompletableFuture<Boolean> remove (K key)

异步,从多映射缓存中删除与密钥关联的条目(如果存在)。

CompletableFuture<Boolean> remove (K key, V value)

如果存在,异步从多映射缓存中删除键值对。

CompletableFuture<Void> remove (Predicate<? super V> p)

异步方法。删除与给定 predicate 匹配的每个值。

CompletableFuture<Boolean> containsKey (K key)

如果此多图包含密钥,则返回 true。

CompletableFuture<Boolean> containsValue (V value)

如果此多图包含至少一个键中的值,则返回 true。

CompletableFuture<Boolean> containsEntry (K key, V value)

如果此多图至少包含一个键值对,则返回 true 异步。

CompletableFuture<Long> size ()

异步,在多映射缓存中返回键值对数量。它不返回不同的键数。

布尔值支持Duplicates ()

如果多映射缓存支持重复,则返回 true 的异步。这意味着 multimap 的内容可以是 'a' ['1', '1', '2']。现在,此方法始终返回 false,因为重复还不被支持。给定值的存在由 'equals' 和 'hashcode' 方法的合同决定。

12.1.3. 创建多映射缓存

目前 MultimapCache 配置为常规缓存。这可以通过代码或 XML 配置来完成。了解如何在 配置 Data Grid 缓存 中配置常规缓存。

12.1.3.1. 嵌入式模式

// create or obtain your EmbeddedCacheManager
EmbeddedCacheManager cm = ... ;

// create or obtain a MultimapCacheManager passing the EmbeddedCacheManager
MultimapCacheManager multimapCacheManager = EmbeddedMultimapCacheManagerFactory.from(cm);

// define the configuration for the multimap cache
multimapCacheManager.defineConfiguration(multimapCacheName, c.build());

// get the multimap cache
multimapCache = multimapCacheManager.get(multimapCacheName);

12.1.4. 限制:

几乎每个情况下 Multimap Cache 的行为都作为常规缓存,但当前版本中存在一些限制,如下所示:

12.1.4.1. 支持重复

尚不支持重复项。这意味着多图不包含任何重复的键值对。当调用 put 方法时,如果键值对已存在,则不会添加此键值 par。用于检查 Multimap 中是否已存在键值对的方法 等于 和 hashcode。

12.1.4.2. Eviction

现在,驱除每个键可以正常工作,而不是每个键值对。这意味着,每当密钥被驱除时,与键关联的所有值也会被驱除。

12.1.4.3. Transactions

通过 auto-commit 支持隐式事务,所有方法都没有阻塞。在大多数情况下,显式事务可以在不阻止的情况下工作。将阻止的大小为 ,containsEntry and remove (Predicate<? super V> p)

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部