I'm having the same problem as you.
Im trying to make a generic kotlin class for reading and populating the cache (as a ConcurrentMap<Key,CompletableFuture) by invoking the passed callback IF the value for the key doesn't exist in the cache.
So when I'm creating an instance for HazelCast (wrapping the value with a CompletableFuture)
companion object {
fun <K, V> create(config: MapConfig, name: String) =
Hazelcast.newHazelcastInstance()
.apply {
this.config.addMapConfig(config)
}.getMap<K, CompletableFuture<V>>(name) //TRYING TO WRAP with CompletableFuture as the AsyncLoad cache does in caffeine
}
Im getting the following exception thrown when trying to read from the cache:
Failed to serialize 'java.util.concurrent.CompletableFuture'
com.hazelcast.nio.serialization.HazelcastSerializationException: Failed to serialize 'java.util.concurrent.CompletableFuture'
at com.hazelcast.internal.serialization.impl.SerializationUtil.handleSerializeException(SerializationUtil.java:115)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toBytes(AbstractSerializationService.java:175)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toBytes(AbstractSerializationService.java:151)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toData(AbstractSerializationService.java:136)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toData(AbstractSerializationService.java:124)
at com.hazelcast.spi.impl.NodeEngineImpl.toData(NodeEngineImpl.java:341)
at com.hazelcast.spi.impl.AbstractDistributedObject.toData(AbstractDistributedObject.java:78)
at com.hazelcast.map.impl.proxy.MapProxyImpl.putIfAbsent(MapProxyImpl.java:172)
at com.hazelcast.map.impl.proxy.MapProxyImpl.putIfAbsent(MapProxyImpl.java:162)
at cache.utility.caffeine.SuspendingCache$get$$inlined$read$1.invokeSuspend(
Here is the HZ API (including the async API for it)
https://docs.hazelcast.org/docs/3.8/javadoc/com/hazelcast/core/IMap.html
I ended up with the following:
(if (cache.containsKey(key)) { //Read operations are thread-safe and are not blocking
return cache.getAsync(key).await() // In Kotlin kotlinx.coroutines.future.await(), in Java you would subscribe from this somehow
} else {
val value = callBack.invoke(key)
cache.setAsync(key,value)
return value
}