2015-01-09 4 views

답변

10

StackExchange.Redis는 이진 안전 인 Redis Strings을 저장할 수 있습니다. 즉, 원하는 직렬화 기술을 사용하여 POCO를 쉽게 직렬화하고 거기에 넣을 수 있습니다.

다음 예제에서는 .NET BinaryFormatter을 사용합니다. 이 작업을하려면 수업을 SerializableAttribute으로 꾸며야합니다. 그것은 BknaryFormatter 좋은 예, 그것은 사용하지 마십시오 제발, 제발입니다

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

확인 응답 – Sherry

+0

주셔서 감사합니다 : 작업을 얻을

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); 

예 :

예는 동작을 설정합니다. 이제까지./cc @Duke –

+0

@MarcGravell 왜? – Frank

관련 문제