This is the customized RedisCodec to save the object as serialized form:
StatefulRedisConnection<String, Object> connect = RedisClient.create(redisUri).connect(
new RedisCodec<String, Object>() {
private final ByteArrayCodec byteArrayCodec = new ByteArrayCodec();
@Override
public String decodeKey(ByteBuffer byteBuffer) {
return Charset.defaultCharset().decode(byteBuffer).toString();
}
@Override
public Object decodeValue(ByteBuffer bytes) {
try (ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(byteArrayCodec.decodeValue(bytes)))) {
return is.readObject();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
@Override
public ByteBuffer encodeKey(String s) {
return Charset.defaultCharset().encode(s);
}
@Override
public ByteBuffer encodeValue(Object o) {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bos)) {
os.writeObject(o);
return byteArrayCodec.encodeValue(bos.toByteArray());
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
}
);
RedisCommands<String, Object> commands = connect.sync();
List<String> input = Arrays.asList("this", "is", "a", "test");
commands.set("test", input);
Object result = commands.get("test");
assert (result.equals(input));