Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

redis - How should i be using Booksleeve with protobuf-net?

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?

Links:

This works but it doesn't feel right:

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

            }
        }
    }
}

More info:

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());
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

All good.

If you make it clear any steps that you find ugly, I might be able to be more specific, but:

  • BookSleeve is entirely async via Task, hence the need for either Wait or ContinueWith to access the byte[]
  • protobuf-net is entirely Stream-based, hence the need for MemoryStream to sit on top of a byte[]

Of course, if you add a generic utility method (maybe an extension method) you only need to write it once.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...