stefanwille / crystal-redis

Full featured Redis client for Crystal
MIT License
380 stars 61 forks source link

What is the recommended way to store small binary data using crystal-redis? #47

Closed da99 closed 6 years ago

da99 commented 6 years ago

If I have binary data (eg MessagePack encoded data), shall I encode it to a string representation for Crystal-Redis?

stefanwille commented 6 years ago

Yes, I think so. I have no idea how to make a nice API using Slice(UInt8) for binary data.

RX14 commented 6 years ago

No, this is wrong. Non-text data in a string is simply incorrect, and creating a String from Bytes causes a memory copy anyway. Encoding the binary data into a String using base64 for example to be valid crystal code takes even more of a performance hit. Please provide an adequate API for binary data.

RX14 commented 6 years ago

I see you already use the binary-safe strings in redis. All you have to do is provide a Bytes overload of this and relax relevent type restrictions to String | Bytes.

def marshal(arg : Bytes, io)
  io << "$" << arg.bytesize << "\r\n"
  io.write(arg)
  io << "\r\n"
end