Search code examples
c#redisprotobuf-netbooksleeve

Basic Booksleeve plus Protobuf-net plus Lists/SortedSets, implementations?


I have some fairly basic questions regarding the use of booksleeve in conjunction with protobuf-net. Now I have implemented a singleton class to manage the connections so I am reusing the same connection many times as recommended. Now I several questions regarding actual use of the combo:

  1. What is the difference/importance of the "db" int and the "key" string?
  2. How would I serialize a bunch of objects into a SortedSet/List using protobuf-net?
  3. How would I deserialize a bunch of objects from a SortedSet/List using protobuf-net?

I was thinking that I should use the Range() method for retrieval:

    public IList<T> RetrieveAllAsList()
    {
        var conn = RedisConnectionManager.Current.GetConnection();
        conn.Open();


        int length = (int)conn.Lists.GetLength(10, "idk").Result;
        byte[][] data = conn.Lists.Range(10, "idk", 0, length-1).Result;

        List<T> output = new List<T>();
        for (int i = 0; i < data.Length; i++)
        {
            using (MemoryStream ms = new MemoryStream(data[i]))
            {
                output.Add(Serializer.Deserialize<T>(ms));
            }
        }

        conn.Close(false);
        return output;
    }

I would appreciate any help in this matter. Thank you.


Solution

  • Sripathi already did a good job of explaining what the DB key is. The reason it is required is that BookSleeve works as a multiplexer, so of you are using a DB it needs to be passed to avoid race conditions when different callers are using different databases. Just pass 0 if you aren't using multiple databases.

    Re serialization/deserialization - this is deliberately done separately, so that the caller can use the appropriate serialization for them - so BookSleeve only knows about strings and blobs. Your serialiazation code looks ok though.

    The only critique I would have is that there is no need to get the length first. The intellisense should illustrate the usage - I expect passing -1 or int.MaxValue will list all the items without needing to get the length first (apologies for being vague, but I'm in a departure lounge). This avoids a network trip.