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

io.github.sinri.keel.cache.temporaryvalue.KeelAsyncTemporaryValue Maven / Gradle / Ivy

Go to download

A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.

The newest version!
package io.github.sinri.keel.cache.temporaryvalue;

import io.github.sinri.keel.cache.ValueWrapper;
import io.vertx.core.Future;

import javax.annotation.Nonnull;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

/**
 * @since 3.0.1
 */
public class KeelAsyncTemporaryValue

{ private final AtomicReference> valueWrapperAtomicReference; private long defaultLifetimeInSeconds = 10L; public KeelAsyncTemporaryValue() { this.valueWrapperAtomicReference = new AtomicReference<>(); } public KeelAsyncTemporaryValue

setDefaultLifetimeInSeconds(long defaultLifetimeInSeconds) { this.defaultLifetimeInSeconds = defaultLifetimeInSeconds; return this; } public Future set(P p) { return set(p, defaultLifetimeInSeconds); } public Future set(P p, long lifeInSeconds) { this.valueWrapperAtomicReference.set(new ValueWrapper<>(p, lifeInSeconds)); return Future.succeededFuture(); } public Future

rawGet() { return Future.succeededFuture(this.valueWrapperAtomicReference.get()) .compose(pValueWrapper -> { if (pValueWrapper == null) { throw new ValueInvalidNow(); } if (!pValueWrapper.isAliveNow()) { throw new ValueInvalidNow(); } return Future.succeededFuture(pValueWrapper.getValue()); }); } public Future

get() { return getOrElse(null); } public Future

getOrElse(P fallback) { return rawGet().recover(throwable -> { return Future.succeededFuture(fallback); }); } public Future

getOrReload(@Nonnull Supplier> loader) { return Future.succeededFuture(this.valueWrapperAtomicReference.get()) .compose(pValueWrapper -> { if (pValueWrapper == null) { throw new ValueInvalidNow(); } if (!pValueWrapper.isAliveNow()) { throw new ValueInvalidNow(); } return Future.succeededFuture(pValueWrapper.getValue()); }) .recover(throwable -> { return loader.get() .compose(p -> { if (p == null) { this.set(p); } return Future.succeededFuture(p); }); }); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy