用Lambdas替换innerclass以执行execute方法和SessionCallback

时间:2017-10-13 05:51:17

标签: java lambda java-8 spring-data spring-data-redis

我有以下用于使用redis进行数据库操作的方法。这目前使用内部类,我得到一个声纳警告用lambda替换它。我们正在使用1.8

List<Object> operationResult = this.stringRedisTemplate.execute(new SessionCallback<List<Object>>() {
                    public List<Object> execute(RedisOperations operations) {

                    List<Object> result = null;
                    try{
                        String key = cacheableDTO.getKey();
                        operations.multi();
                        ValueOperations<String, String> valueOperations = operations.opsForValue();

                        //add the value in cache
                        if(cacheableDTO.getTtlInSeconds() <= 0) {
                            valueOperations.set(key, jsonData);
                        } else {
                            valueOperations.set(key, jsonData, cacheableDTO.getTtlInSeconds(), TimeUnit.SECONDS);
                        }

                        //add the key to the keys_set
                        operations.opsForSet().add(FEO_MS_KEYS_SET, key);
                    } catch (Exception e) {
                        LOGGER.error("Failed to async add CacheableDTO to cache [" + cacheableDTO + "]", e);
                    } finally {
                        result = operations.exec();
                    }
                    return result;
                }
                }
            );

但是,SessionCallback的实现如下 公共接口SessionCallback {

    /**
     * Executes all the given operations inside the same session.
     * 
     * @param operations Redis operations
     * @return return value
     */
    <K, V> T execute(RedisOperations<K, V> operations) throws DataAccessException;
}

RedisTemplate重载了执行

public <T> T execute(RedisCallback<T> action)

public <T> T execute(SessionCallback<T> session)

我尝试使用lambda

List<Object> operationResult = this.stringRedisTemplate.execute(
                (RedisOperations operations)-> {

                    List<Object> result = null;
                    try{

但是这并没有映射到正确的执行,它提供的错误参数应该是RedisConnection类型。

另外,如果我拿出内部类并尝试将其映射到lambda

SessionCallback<List<Object>> sessionCallback = (RedisOperations operations)-> {


                List<Object> result = null;
                try{..

SessionCallback<List<Object>> sessionCallback = (RedisOperations<String,String> operations)

我收到一个错误,该方法执行的Sessin Callback类型是通用的。是因为通用参数K,V还是我想念的东西。

由于

0 个答案:

没有答案