1

I have following method that we use for database operations with redis. This at present uses innerclass and i got an Sonar Warning to replace it with lambda. We are using 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;
                }
                }
            );

However, SessionCallback implementation is as follows public interface 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 has overloaded execute

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

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

I tried to use lambda

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

                    List<Object> result = null;
                    try{

But this doesn't map to the correct execute and it gives error parameter is expected to be of type RedisConnection.

Also, if i take the inner class out and try to map it to lambda

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


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

or

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

i get an error that the method execute of type Sessin Callback is generic. Is it because the generic params K,V or something a miss from my side.

Thanks

erran
  • 1,300
  • 2
  • 15
  • 36
user1643003
  • 255
  • 4
  • 16
  • Possible duplicate of [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Joe C Oct 13 '17 at 06:21
  • I don't think this is a duplicate. This refers to replacing initialization of a spring-data-redis `SessionCallback` with a lambda function. I ran into this today myself. – erran May 08 '18 at 23:26

0 Answers0