我正在尝试使用两个著名的 C# 驱动程序ServiceStack和StackExchange来评估 Redis 。不幸的是,我不能使用 ServiceStack,因为它不是免费的。现在我正在尝试 StackExchange。
有人知道我是否可以使用 StackExchange.Redis 坚持 POCO?
我正在尝试使用两个著名的 C# 驱动程序ServiceStack和StackExchange来评估 Redis 。不幸的是,我不能使用 ServiceStack,因为它不是免费的。现在我正在尝试 StackExchange。
有人知道我是否可以使用 StackExchange.Redis 坚持 POCO?
StackExchange.Redis can store Redis Strings, which are binary safe. That means, that you can easily serialize a POCO using the serialization technology of your choice and put it in there.
The following example uses the .NET BinaryFormatter. Please note that you have to decorate your class with the SerializableAttribute
to make this work.
Example set operation:
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);
Example get operation:
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);
}
}