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