17

I want to use the MessagePack, ZeroFormatter or protobuf-net to serialize/deserialize a generic list and store it in Redis using the stackexchange.redis client.

Now I'm storing a JSON string with the StringSetAsync() method. But I can't find any documentation on how to store a byte[] in Redis.

1 Answer 1

31

StackExchange.Redis uses RedisValue to represent different types of values stored in Redis and so it provides implicit conversion operators (for byte[] among others). Please read StackExchange.Redis / Basic Usage / Values carefully as in the third sentence of that chapter you can find

However, in addition to text and binary contents, ...

which basically means that you can use IDatabase.StringSet() to store a basic value (which Redis generally thinks of as a "string" as there are other types like sets, hashes, and so on) - be it string or an array of bytes.

        using (var multiplexer = ConnectionMultiplexer.Connect("localhost:6379"))
        {
            byte[] byteArray = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
            var db = multiplexer.GetDatabase();
            db.StringSet("bytearray", byteArray);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

(nods in appreciation)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.