Som*_*des 15 c# redis servicestack.redis stackexchange.redis
我试图通过使用两个众所周知的C#驱动程序ServiceStack和StackExchange来评估Redis .不幸的是我无法使用ServiceStack,因为它不是免费的.现在我正在尝试StackExchange.
有人知道是否使用StackExchange.Redis我可以坚持POCO吗?
Fra*_*ank 18
StackExchange.Redis可以存储Redis字符串,它是二进制安全的.这意味着,您可以使用您选择的序列化技术轻松地序列化POCO并将其放入其中.
以下示例使用.NET BinaryFormatter.请注意,您必须使用它来装饰您的课程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);
Run Code Online (Sandbox Code Playgroud)
示例获取操作:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6257 次 |
| 最近记录: |