play.cache.DefaultAsyncCacheApi Maven / Gradle / Ivy
/*
* Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc.
*/
package play.cache;
import static scala.jdk.javaapi.FutureConverters.asJava;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.pekko.Done;
import play.libs.Scala;
import scala.concurrent.duration.Duration;
import scala.jdk.javaapi.OptionConverters;
/**
* Adapts a Scala AsyncCacheApi to a Java AsyncCacheApi. This is Play's default Java AsyncCacheApi
* implementation.
*/
@Singleton
public class DefaultAsyncCacheApi implements AsyncCacheApi {
private final play.api.cache.AsyncCacheApi asyncCacheApi;
@Inject
public DefaultAsyncCacheApi(play.api.cache.AsyncCacheApi cacheApi) {
this.asyncCacheApi = cacheApi;
}
@Override
public SyncCacheApi sync() {
return new SyncCacheApiAdapter(asyncCacheApi.sync());
}
@Override
public CompletionStage> get(String key) {
return asJava(asyncCacheApi.get(key, Scala.classTag())).thenApply(OptionConverters::toJava);
}
@Override
public CompletionStage getOrElseUpdate(
String key, Callable> block, int expiration) {
return asJava(
asyncCacheApi.getOrElseUpdate(
key, intToDuration(expiration), Scala.asScalaWithFuture(block), Scala.classTag()));
}
@Override
public CompletionStage getOrElseUpdate(String key, Callable> block) {
return asJava(
asyncCacheApi.getOrElseUpdate(
key, Duration.Inf(), Scala.asScalaWithFuture(block), Scala.classTag()));
}
@Override
public CompletionStage