StackExchange.RedisはバイナリセーフであるRedis Stringsを格納できます。つまり、選択したシリアライズテクノロジを使用してPOCOを簡単にシリアル化し、そこに配置することができます。
次の例では、.NETを使用しています。この作業を行うには、SerializableAttribute
でクラスを飾る必要があります。
例の動作を設定します。
PocoType somePoco = new PocoType { Id = 1, Name = "YouNameIt" };
string key = "myObject1";
byte[] bytes;
using (var stream = new MemoryStream())
{
new BinaryFormatter().Serialize(stream, somePoco);
bytes = stream.ToArray();
}
db.StringSet(key, bytes);
例の動作を取得:
string key = "myObject1";
PocoType somePoco = null;
byte[] bytes = (byte[])db.StringGet(key);
if (bytes != null)
{
using (var stream = new MemoryStream(bytes))
{
somePoco = (PocoType) new BinaryFormatter().Deserialize(stream);
}
}
OKをそれが良い例である応答 – Sherry
をありがとうございました、それはBknaryFormatterを使用しないでくださいくださいください。これまで/ cc @Duke –
@MarcGravellなぜですか? – Frank