Created
July 13, 2019 06:37
-
-
Save austinhaha/d93efcca966c4b6fb8066c2a1c395828 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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