我应该如何将 Booksleeve 与 protobuf-net 一起使用? [英] How should i be using Booksleeve with protobuf-net?
问题描述
我使用 RedisConnection Set 方法来设置字节数组,但如何获取数据?get 返回一个包装好的字节数组?
I use the RedisConnection Set method to set the byte array but how do i get the data? The get returns a wrapped byte array?
链接:
这有效,但感觉不对:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookSleeve;
using ProtoBuf;
using System.IO;
namespace RedisTest001
{
[ProtoContract, Serializable]
public class Price
{
private string _ticker;
private double _value;
public Price()
{
}
public Price(string ticker, double value)
{
_ticker = ticker;
_value = value;
}
[ProtoMember(1)]
public string Ticker
{
get { return _ticker; }
set { _ticker = value; }
}
[ProtoMember(2)]
public double Value
{
get { return _value; }
set { _value = value; }
}
}
class Program
{
static void Main(string[] args)
{
using (var conn = new RedisConnection("localhost"))
{
Price p = new Price("IBM", 101.55);
byte[] raw;
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize<Price>(ms,p);
raw = ms.ToArray();
}
conn.Open();
conn.Set(1, p.Ticker, raw);
var tb = conn.Get(1,"IBM");
var str = conn.Wait(tb);
Price p2 = Serializer.Deserialize<Price>(new MemoryStream(str));
}
}
}
}
更多信息:
public static class pex
{
public static byte[] ToBArray<T>(this T o)
{
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize<T>(ms, o);
return ms.ToArray();
}
}
}
class Program
{
static void Main(string[] args)
{
Random RandomClass = new Random();
using (var conn = new RedisConnection("localhost"))
{
conn.Open();
for (int i = 0; i < 500000; i++)
{
Price p = new Price("IBM", RandomClass.Next(0, 1000));
conn.AddToSet(2, "PRICE.IBM", p.ToBArray());
}
推荐答案
这是完全正确的."Get" (BookSleeve) 返回一个延迟的 byte[]
.您已正确使用 Wait 来获取实际的 byte[]
,然后在此 byte[]
上使用 MemoryStream
来调用 Deserialize
通过 protobuf-net.
That is entirely correct. "Get" (BookSleeve) returns a deferred byte[]
. You have correctly used Wait to get the actual byte[]
, then used a MemoryStream
over this byte[]
to call Deserialize
via protobuf-net.
一切都好.
如果你说清楚你觉得难看的任何步骤,我可能会更具体,但是:
If you make it clear any steps that you find ugly, I might be able to be more specific, but:
- BookSleeve 通过
Task
完全异步,因此需要Wait
或ContinueWith
来访问byte[]代码>
- protobuf-net 完全基于 Stream,因此需要
MemoryStream
位于byte[]
之上
- BookSleeve is entirely async via
Task
, hence the need for eitherWait
orContinueWith
to access thebyte[]
- protobuf-net is entirely Stream-based, hence the need for
MemoryStream
to sit on top of abyte[]
当然,如果你添加一个通用的实用方法(可能是一个扩展方法),你只需要编写一次.
Of course, if you add a generic utility method (maybe an extension method) you only need to write it once.
如果添加了一个包装类(用于某些跟踪/滑动到期)和一个 L1 缓存(Redis 作为 L2),这几乎就是我们在 stackoverflow 中使用它的方式.
And with the addition if a wrapper class (for some tracking/sliding-expiry) and a L1 cache (Redis as L2), this is pretty much exacty how we use it at stackoverflow.
注意:连接是线程安全的,旨在大规模共享;不要每次操作都建立连接.
One note: the connection is thread safe and intended to be massively shared; don't do a connection per operation.
这篇关于我应该如何将 Booksleeve 与 protobuf-net 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!