1

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.

Community
  • 1
  • 1
tamaslnagy
  • 431
  • 1
  • 7
  • 16

2 Answers2

2

What is the difference/importance of the "db" int and the "key" string?

Redis allows you to partition your data into several databases. For example, on the stackexchange websites, you may store stackoverflow.com related stuff in db=0, and programmers.se.com related stuff in db = 1.

Within each database, you have key = value pairs. The key is always a String. The value can be one of the following five data types - String, List, Set, Sorted Set or a Map.

A traditional Redis client does not force you to provide the database number. If you don't provide one, it assumes db=0. But Booksleeve requires you to provide the database number. If you don't care, just pass 0 to all API calls.

The string key must be unique though, and is totally application specific. For example, to store a user object, the usual technique is to have a key like user:1190099, and the value as a Map with key=value pairs like {"name":"singlelabs", "id":1190099 ... }

How would I serialize a bunch of objects into a SortedSet/List using protobuf-net? How would I deserialize a bunch of objects from a SortedSet/List using protobuf-net?

First, you need to decide whether you want to use protocol buffers or not.

Protocol Buffers can serialize/deserialize complex objects into a binary blob. This blob is opaque to Redis. In other words, you can set or get this binary blob, but you cannot use any other Redis features.

If you really want to use Redis' lists and sorted sets, you should use the APIs provided by BookSleeve. I haven't used Booksleeve, but the following two interfaces should explain how to insert data into a list / sortedset.

http://code.google.com/p/booksleeve/source/browse/BookSleeve/ISortedSetCommands.cs

http://code.google.com/p/booksleeve/source/browse/BookSleeve/IListCommands.cs

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
Sripathi Krishnan
  • 30,948
  • 4
  • 76
  • 83
1

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • If I wanted BookSleeve to return a maximum of 250 items in a list, I would need to pass 250 as the fourth parameter of Range(), correct? Also, is there any better way of serializing? Because this just feels inefficient. Thanks for your help. – tamaslnagy Apr 17 '12 at 18:41
  • @singelabs right; re serialising... I really doubt that is going to be a bottleneck – Marc Gravell Apr 18 '12 at 12:20