Home > Back-end >  How to use RedisTemplate storage read byte [] (by protobuf serialized as the byte [])?
How to use RedisTemplate storage read byte [] (by protobuf serialized as the byte [])?

Time:09-24

Use the string as the key, with a byte [] as the value

Change how to implement in RedisTemplate, please?

Had meant to be, write a spring redis class, can be protobuf message corresponding byte [] conversion


But I found that seemed not to protobuf message of abstract superclass GeneratedMessage as well as the implementation of the interface MessageOrBuilder don't contain parseFrom function

I don't know which master have what way,

Or, not really, how should I write the class or what can I use the string as the key, with a byte [] as the value read, written by the protobuf serialized as the byte []

CodePudding user response:

hey, alone to write a byte [] of the parser can solve this problem, but please everybody to give directions, is there any way to achieve what I want, directly above the protubuf class to byte [] in the parser directly convert?

 public class ByteRedisSerializer implements RedisSerializerPrivate final Charset Charset. 

Public ByteRedisSerializer () {
This (Charset. Class.forname (" UTF8 "));
}

Public ByteRedisSerializer (Charset Charset) {
Assert. NotNull (charset, "charset must not be null!" );
This. Charset=charset;
}

@ Override
Public byte [] serialize (byte [] t) throws SerializationException {
Return t;
}

@ Override
Public byte [] deserialize (byte [] bytes) throws SerializationException {
Return bytes;
}

}

CodePudding user response:

Your key is byte array not to go, why must the string

CodePudding user response:

The problem is not difficult to
At the time of serialization, in front of the protobuf instance with a type of the message, add a header to separate type and content:

 
Serialize (T T) {
Byte [] clzBytes=t.g etClass (), toString (). GetBytes ();//remove storage protobuf actual type and serialization
Byte [] contentBytes=t.t oByteArray ();//content serialization

Byte [] fullPacket=Arrays. CopyOf (new byte [] {((Integer) clzBytes. Length). ByteValue ()}, 1 + clzBytes. Length + contentBytes. Length);
System. Arraycopy (fullPacket clzBytes, 0, 1, clzBytes length);
System. Arraycopy (fullPacket contentBytes, 0, 1 + clzBytes. Length, contentBytes. Length);

Return fullPacket;
}


Then at the time of decoding, first solutions first byte to obtain the length of the class, in solving a class of type, the reflex instance by class parseFrom content body
  • Related