Here's my basic example setup:
private final RedisTemplate<String, Object> redisTemplate;
public void save(Object thing, Instant expiresAt) {
redisTemplate.execute(new SessionCallback<Void>() {
@Override
public @Nullable Void execute(@NonNull RedisOperations ops) throws DataAccessException {
ops.opsForValue("key", thing);
ops.expireAt("key", expiresAt);
return null;
}
});
}
That version compiles, but has multiple warnings for
Unchecked call to ... as a member of raw type ...
These attempts all fail to compile:
redisTemplate.execute((SessionCallback<Void>) ops -> {
ops.opsForValue("key", thing);
ops.expireAt("key", expiresAt);
return null;
});
Target method is generic
redisTemplate.execute(new SessionCallback<Void>() {
@Override
public @Nullable Void execute(@NonNull RedisOperations<String, Object> ops) throws DataAccessException {
ops.opsForValue("key", thing);
ops.expireAt("key", expiresAt);
return null;
}
});
Class 'Anonymous class derived from SessionCallback' must implement abstract method 'execute(RedisOperations<K, V>)' in 'SessionCallback'
redisTemplate.execute(new SessionCallback<Void>() {
@Override
public <K, V> @Nullable Void execute(@NonNull RedisOperations<K, V> ops) throws DataAccessException {
ops.opsForValue("key", thing);
ops.expireAt("key", expiresAt);
return null;
}
});
Required type 'T', provided 'String'
Cannot resolve method 'expireAt(String, Instant)'
And finally, compiling with warnings:
redisTemplate.execute(new SessionCallback<Void>() {
@Override
public <K, V> @Nullable Void execute(@NonNull RedisOperations<K, V> ops) throws DataAccessException {
ops.opsForValue((K) "key", (V) thing);
ops.expireAt((K) "key", (V) expiresAt);
return null;
}
});
Unchecked cast: 'java.lang.String' to 'K'
Is it actually possible to use this API without type warnings or errors?
-
2You don't. There are several issues registered for this in the Spring Data Redis repository.M. Deinum– M. Deinum2025年04月14日 13:51:00 +00:00Commented Apr 14 at 13:51
-
Ah yes, github.com/spring-projects/spring-data-redis/issues/815 from 2013...OrangeDog– OrangeDog2025年04月14日 13:55:29 +00:00Commented Apr 14 at 13:55