[id='using-multimap-cache_{context}']
= Multimap Cache

MutimapCache is a type of {brandname} Cache that maps keys to values in which each key can contain multiple values.

== Installation and configuration

.pom.xml
[source,xml,options="nowrap",subs=attributes+]
----
include::dependencies_maven/multimap.xml[]
----

== MultimapCache API

MultimapCache API exposes several methods to interact with the Multimap Cache.
These methods are non-blocking in most cases; see
link:#multimap_limitations[limitations] for more information.

[source,java]
----
include::code_examples/MultiMapCache.java[]
----

.CompletableFuture<Void> put(K key, V value)
Puts a key-value pair in the multimap cache.

[source,java]
----
include::code_examples/MultiMapCachePut.java[]
----

The output of this code is as follows:

[source, txt]
----
Marie is a girl name
Oihana is a girl name
----

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

Asynchronous that returns a view collection of the values associated with key in this multimap cache, if any. Any changes to the retrieved collection won't change the values in this multimap cache.
When this method returns an empty collection, it means the key was not found.

.CompletableFuture<Boolean> remove(K key)
Asynchronous that removes the entry associated with the key from the multimap cache, if such exists.

.CompletableFuture<Boolean> remove(K key, V value)
Asynchronous that removes a key-value pair from the multimap cache, if such exists.

.CompletableFuture<Void> remove(Predicate<? super V> p)
Asynchronous method. Removes every value that match the given predicate.

.CompletableFuture<Boolean> containsKey(K key)
Asynchronous that returns true if this multimap contains the key.

.CompletableFuture<Boolean> containsValue(V value)
Asynchronous that returns true if this multimap contains the value in at least one key.

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

Asynchronous that returns true if this multimap contains at least one key-value pair with the value.

.CompletableFuture<Long> size()
Asynchronous that returns the number of key-value pairs in the multimap cache. It doesn't return the distinct number of keys.

.boolean supportsDuplicates()
Asynchronous that returns true if the multimap cache supports duplicates. This means that the content of the multimap can be
'a' -> ['1', '1', '2']. For now this method will always return false, as duplicates are not yet supported.
The existence of a given value is determined by 'equals' and `hashcode' method's contract.

== Creating a Multimap Cache

Currently the MultimapCache is configured as a regular cache. This can be done either by code or XML configuration.
See how to configure a regular cache in link:{config_docs}#cache-configuration[Configuring {brandname} caches].

=== Embedded mode

[source,java]
----
include::code_examples/MultiMapCacheEmbedded.java[]
----

[[multimap_limitations]]
== Limitations

In almost every case the Multimap Cache will behave as a regular Cache, but some limitations exist in the current version, as follows:

=== Support for duplicates
A multimap can be configured to store duplicate values for a single key. A duplicate is determined by the value's `equals` method.
Whenever the put method is called, if multimap is configured to support duplicates, the key-value pair will be added to the collection.
Invoking remove on the multimap will remove all duplicates if present.

=== Eviction

For now, the eviction works per key, and not per key-value pair.
This means that whenever a key is evicted, all the values associated with the key will be evicted too.

=== Transactions

Implicit transactions are supported through the auto-commit and all the methods are non blocking.
Explicit transactions work without blocking in most of the cases.
Methods that will block are `size`, `containsEntry` and `remove(Predicate<? super V> p)`
