简体   繁体   English

Java Futures和Callable中具有类型检查的泛型方法实现

[英]Generic method implementation with type checking in Java Futures and Callable

I am trying to add a custom cache layer using HazelCast, but I am not sure how to implement the method below 我正在尝试使用HazelCast添加自定义缓存层,但是我不确定如何实现以下方法

public <T> CompletionStage<T> getOrElseUpdate(String key, Callable<CompletionStage<T>> callable, int expiration)

Below is my full class. 以下是我的全班课。 The contract of the above method is to find item based on key from cache else execute the block and return a completion stage with added item in it. 上述方法的约定是从缓存中基于键查找项目,或者执行该块并返回其中添加了项目的完成阶段。

I have tried few ways of doing it, but lot of type erasure errors and non-conversion of Callable<CompletionStage<T>> has had me confused. 我尝试了几种方法,但是很多类型擦除错误和Callable<CompletionStage<T>>未转换使我感到困惑。

import akka.Done;
import com.hazelcast.core.IMap;
import play.cache.AsyncCacheApi;

import java.util.concurrent.*;

import static java.util.concurrent.CompletableFuture.runAsync;
import static java.util.concurrent.CompletableFuture.supplyAsync;

public class MyAsyncCache<T> implements AsyncCacheApi {

    IMap<String, T> internalMapCache;

    @Override
    public <T> CompletionStage<T> get(String key) {
        return supplyAsync( () -> {
            T t = (T) internalMapCache.get(key);
            return t;});

    }

    @Override
    public <T> CompletionStage<T> getOrElseUpdate(String key, Callable<CompletionStage<T>> callable, int expiration) {

        supplyAsync( () -> {
            T t = (T) internalMapCache.get(key);
            if(t == null){
                //callable.call()
            }else {
                return t;
            }
        }

        );
    }

    @Override
    public <T> CompletionStage<T> getOrElseUpdate(String key, Callable<CompletionStage<T>> block) {

    }

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

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

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

    @Override
    public CompletionStage<Done> removeAll() {
        return supplyAsync( () -> {
            internalMapCache.clear();
            return Done.getInstance();
        });
    }

} }

Any help, leads to resolve this will greatly be appreciated. 任何帮助,导致解决此问题将不胜感激。


Update 更新

  1. Removed the type from the class MyAsyncCache<T> doesn't need to be generic at that level so its only MyAsyncCache now 从类MyAsyncCache<T>中删除该类型不需要在该级别上是通用的,因此现在它只有MyAsyncCache
  2. I have added my implementation of the method getOrElseUpdate as below but I have a feeling it can be improved yet. 我如下添加了方法getOrElseUpdate实现,但是我觉得它可以改进。

The method updated as below: 方法更新如下:

@Override
public <T> CompletionStage<T> getOrElseUpdate(String key, Callable<CompletionStage<T>> callable, int expiration) {
    CompletableFuture<T> uCompletableFuture = supplyAsync(() -> {
        T obj;
        if (!mapBased.containsKey(key)) {
            try {
                Future<CompletionStage<T>> submit = Executors.newSingleThreadExecutor()
                                                             .submit(callable);
                obj = submit.get().toCompletableFuture().get();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } else {
           obj = (T) mapBased.get(key);
        }
        return obj;
    });
    return uCompletableFuture;
}

How can I improve on the above method ? 如何改善上述方法?

T in in

public class MyAsyncCache<T> implements AsyncCacheApi {

and T in 和T in

public <T> CompletionStage<T> get(String key) {

are different. 是不同的。 Use different letters for them. 为它们使用不同的字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM