Skip to content

Instantly share code, notes, and snippets.

@austinhaha
Created July 13, 2019 06:37
Show Gist options
  • Save austinhaha/d93efcca966c4b6fb8066c2a1c395828 to your computer and use it in GitHub Desktop.
Save austinhaha/d93efcca966c4b6fb8066c2a1c395828 to your computer and use it in GitHub Desktop.
import com.google.gson.Gson;
import io.lettuce.core.codec.RedisCodec;
import lombok.RequiredArgsConstructor;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
@RequiredArgsConstructor
public final class ValuxCodec<T> implements RedisCodec<String, T> {
private static final Charset CHARSET = Charset.forName("UTF-8");
private final Gson gson;
private final Class<T> typeParam;
@Override
public String decodeKey(ByteBuffer byteBuffer) {
return CHARSET.decode(byteBuffer).toString();
}
@Override
public T decodeValue(ByteBuffer byteBuffer) {
String json = CHARSET.decode(byteBuffer).toString();
return gson.fromJson(json, typeParam);
}
@Override
public ByteBuffer encodeKey(String s) {
return CHARSET.encode(s);
}
@Override
public ByteBuffer encodeValue(T t) {
return CHARSET.encode(gson.toJson(t));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment