-
Hello! I have a question regarding implementing a custom codec. I am using lettuce to read a gzip-compressed string in Redis and deserializing the said string in a Scala application using Currently we are reading the gzip data into a String then call Now I have implemented a custom codec (for reads only) that returns an public class StringInputStreamCodec implements RedisCodec<String, InputStream> {
private final Charset charset = StandardCharsets.UTF_8;
@Override
public String decodeKey(final ByteBuffer bytes) {
return charset.decode(bytes).toString();
}
@Override
public InputStream decodeValue(final ByteBuffer bytes) {
return new ByteBufferBackedInputStream(bytes);
}
@Override
public ByteBuffer encodeKey(final String key) {
return charset.encode(key);
}
@Override
public ByteBuffer encodeValue(final InputStream value) {
throw new NotImplementedException("This is a read-only codec!");
}
} And the Usage: val conn = client.connect(CompressionCodec.valueCompressor(
new StringInputStreamCodec(), CompressionCodec.CompressionType.GZIP)).async(); The question: my tests shows that it is working but...is this a sound approach? I'm reading here about the Codecs being thread-safe but it didn't warn anything about NOT referencing the underlying implementation of the data returned as Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I converted the issue into a discussion. The |
Beta Was this translation helpful? Give feedback.
I converted the issue into a discussion.
The
ByteBuffer
interface in codecs is used by Lettuce accepting and providing buffers. When decoding, you should either copy or consume buffers as the input buffer is attached to a Netty buffer. Keeping a reference may result in a changed underlying buffer and undesired consequences.