使用AsyncCacheApi播放框架|在Java中实现测试缓存

使用AsyncCacheApi播放框架|在Java中实现测试缓存,java,scala,caching,playframework,Java,Scala,Caching,Playframework,我目前正在尝试在Java中实现play.api.cache.AsyncCacheApi。由于泛型参数,我在实现get和GetOrelsUpdate时遇到了很大的困难 我的班级: package common; import akka.Done; import net.sf.ehcache.Element; import play.api.cache.AsyncCacheApi; import play.api.cache.SyncCacheApi; import scala.Function0

我目前正在尝试在Java中实现play.api.cache.AsyncCacheApi。由于泛型参数,我在实现get和GetOrelsUpdate时遇到了很大的困难

我的班级:

package common;

import akka.Done;
import net.sf.ehcache.Element;
import play.api.cache.AsyncCacheApi;
import play.api.cache.SyncCacheApi;
import scala.Function0;
import scala.Option;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import scala.reflect.ClassTag;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static scala.compat.java8.FutureConverters.toScala;

public class InMemoryCacheJava implements AsyncCacheApi {

   final Map<String, Element> cache = new HashMap<>();

   @Override
   public SyncCacheApi sync() {
       return null;
   }

   @Override
   public Future<Done> set(String key, Object value, Duration expiration) {
       return toScala(CompletableFuture.supplyAsync(
           () -> {
               final Element element = new Element(key, value);
               if (expiration == Duration.Zero()) {
                   element.setEternal(true);
               }
               element.setTimeToLive(Math.toIntExact(expiration.toSeconds()));
               cache.put(key, element);
               return Done.getInstance();
           }
       ));
   }

   @Override
   public Future<Done> remove(String key) {
       return toScala(CompletableFuture.supplyAsync(
           () -> {
               cache.remove(key);
               return Done.getInstance();
           }
       ));
   }

   @Override
   public <T> Future<Option<T>> get(String key, ClassTag<T> evidence$2) {
       return null;
   }

   @Override
   public Future<Done> removeAll() {
       return toScala(CompletableFuture.supplyAsync(
           () -> {
               cache.clear();
               return Done.getInstance();
           }
       ));
   }

   @Override
   public <A> Future<A> getOrElseUpdate(String key, Duration expiration, Function0<Future<A>> orElse, ClassTag<A> evidence$1) {
       return null;
   }

}

包通用;
进口阿克卡。完成;
导入net.sf.ehcache.Element;
导入play.api.cache.AsyncCacheApi;
导入play.api.cache.SyncCacheApi;
导入scala.Function0;
导入scala.Option;
导入scala.concurrent.Future;
导入scala.concurrent.duration.duration;
导入scala.reflect.ClassTag;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Optional;
导入java.util.concurrent.CompletableFuture;
导入静态scala.compat.java8.futurecoverters.toScala;
MemoryCacheJava中的公共类实现了AsyncCacheApi{
最终映射缓存=新HashMap();
@凌驾
公共SyncCacheApi sync(){
返回null;
}
@凌驾
公共未来集(字符串键、对象值、持续时间到期){
返回到Cala(CompletableFuture.SupplySync(
() -> {
最终元素=新元素(键、值);
if(过期==持续时间.Zero()){
元素。setexternal(true);
}
setTimeToLive(Math.toIntExact(expiration.toSeconds());
cache.put(键、元素);
返回Done.getInstance();
}
));
}
@凌驾
公共未来删除(字符串键){
返回到Cala(CompletableFuture.SupplySync(
() -> {
缓存。删除(键);
返回Done.getInstance();
}
));
}
@凌驾
公共未来获取(字符串键、类标记证据$2){
返回null;
}
@凌驾
公共未来移除{
返回到Cala(CompletableFuture.SupplySync(
() -> {
cache.clear();
返回Done.getInstance();
}
));
}
@凌驾
公共未来GetOrelsUpdate(字符串键、持续时间到期、函数0 orElse、类标记证据$1){
返回null;
}
}

这样做对吗?我之所以使用此API,是因为在我的生产代码中,我使用的是DefaultAsyncCacheApi实现,我希望使用内部缓存(如映射)测试我的代码。我缺少什么?

我决定改用Java API,并实现了自己的内存缓存

我的工作代码(可能不是最好的实现),如果它可以帮助:

package common;

import akka.Done;
import play.cache.AsyncCacheApi;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public class InMemoryCache implements AsyncCacheApi {
    private final HashMap<String, Object> cache = new HashMap();

    @Override
    public <T> CompletionStage<T> get(String key) {
        return CompletableFuture.completedFuture((T) this.cache.get(key));
    }

    @Override
    public <T> CompletionStage<T> getOrElseUpdate(String key, Callable<CompletionStage<T>> block, int expiration) {
        return this.getOrElseUpdate(key, block);
    }

    @Override
    public <T> CompletionStage<T> getOrElseUpdate(String key, Callable<CompletionStage<T>> block) {
        final Object o = this.cache.get(key);
        if (Objects.isNull(o)) {
            try {
                return block.call();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return CompletableFuture.completedFuture((T) o);
    }

    @Override
    public CompletionStage<Done> set(String key, Object value, int expiration) {
        return this.set(key, value);
    }

    @Override
    public CompletionStage<Done> set(String key, Object value) {
        return CompletableFuture.supplyAsync(
            () -> {
                this.cache.put(key, value);
                return Done.getInstance();
            }
        );
    }

    @Override
    public CompletionStage<Done> remove(String key) {
        return CompletableFuture.supplyAsync(
            () -> {
                this.cache.remove(key);
                return Done.getInstance();
            }
        );
    }

    @Override
    public CompletionStage<Done> removeAll() {
        return CompletableFuture.supplyAsync(() -> {
            this.cache.clear();
            return Done.getInstance();
        });
    }
}
包通用;
进口阿克卡。完成;
导入play.cache.AsyncCacheApi;
导入java.util.HashMap;
导入java.util.Objects;
导入java.util.concurrent.Callable;
导入java.util.concurrent.CompletableFuture;
导入java.util.concurrent.CompletionStage;
MemoryCache中的公共类实现了AsyncCacheApi{
私有最终HashMap缓存=新HashMap();
@凌驾
公共CompletionStage获取(字符串键){
返回CompletableFuture.completedFuture((T)this.cache.get(key));
}
@凌驾
public CompletionStage GetOrelsUpdate(字符串键、可调用块、整数过期){
返回此.getOrElseUpdate(键,块);
}
@凌驾
public CompletionStage GetOrelsUpdate(字符串键,可调用块){
最终对象o=this.cache.get(key);
if(Objects.isNull(o)){
试一试{
返回block.call();
}捕获(例外e){
e、 printStackTrace();
}
}
返回CompletableFuture.completedFuture((T)o);
}
@凌驾
公共CompletionStage集(字符串键、对象值、int过期){
返回此.set(键、值);
}
@凌驾
公共CompletionStage集(字符串键、对象值){
返回CompletableFuture.SupplySync(
() -> {
this.cache.put(key,value);
返回Done.getInstance();
}
);
}
@凌驾
公共CompletionStage删除(字符串键){
返回CompletableFuture.SupplySync(
() -> {
this.cache.remove(key);
返回Done.getInstance();
}
);
}
@凌驾
public CompletionStage removeAll(){
返回CompletableFuture.SupplySync(()->{
this.cache.clear();
返回Done.getInstance();
});
}
}