2015-01-09 13 views

答えて

10

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); 
    } 
} 
+0

OKをそれが良い例である応答 – Sherry

+0

をありがとうございました、それはBknaryFormatterを使用しないでくださいくださいください。これまで/ cc @Duke –

+0

@MarcGravellなぜですか? – Frank

関連する問題