All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.up.uca.cache.RapidDict Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.uca.cache;

import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.up.atom.Kv;
import io.vertx.up.unity.Ux;
import io.vertx.up.util.Ut;

import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

/**
 * @author Lang
 */
class RapidDict extends AbstractRapid, ConcurrentMap> {
    RapidDict(final String cacheKey, final int expired) {
        super(cacheKey, expired);
    }

    @Override
    public Future> cached(
        final Set keys,
        final Function, Future>> executor) {
        Objects.requireNonNull(keys);
        return this.pool.get(keys).compose(dataMap -> {
            final Set keySet = Ut.diff(keys, dataMap.keySet());
            final ConcurrentMap cached = new ConcurrentHashMap<>(dataMap);
            if (keySet.isEmpty()) {
                return Ux.future(cached);
            } else {
                return executor.apply(keySet).compose(queried -> {
                    final ConcurrentMap> futureMap = new ConcurrentHashMap<>();
                    queried.forEach((key, data) ->
                        futureMap.put(key, this.pool.put(key, data, this.expired).compose(Kv::value)));
                    return Ux.thenCombine(futureMap);
                }).compose(newMap -> {
                    cached.putAll(newMap);
                    return Ux.future(cached);
                });
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy