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

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 set(String key, Object value, int expiration) {
    return asJava(asyncCacheApi.set(key, value, intToDuration(expiration)));
  }

  @Override
  public CompletionStage set(String key, Object value) {
    return asJava(asyncCacheApi.set(key, value, Duration.Inf()));
  }

  @Override
  public CompletionStage remove(String key) {
    return asJava(asyncCacheApi.remove(key));
  }

  @Override
  public CompletionStage removeAll() {
    return asJava(asyncCacheApi.removeAll());
  }

  private Duration intToDuration(int seconds) {
    return seconds == 0 ? Duration.Inf() : Duration.apply(seconds, TimeUnit.SECONDS);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy