Search code examples
c#redisbooksleeve

Add multiple member - score values not supported in Redis Booksleeve


I'm trying to store multiple member-score values using Redis Booksleeve but the Booksleeve api doesn't seem to support this functionality.

conn.SortedSets.Add overload supports only single pairs of value - scores.

Do i miss something or this is on purpose or something that you forgot to implement?

Currently i'm updating my sorted set in a transaction loop like this:

foreach (ForumMessage message in messages)
{
    trans.SortedSets.Add(db, redisKey, message.id.ToString(), message.id);
}
trans.Execute();

Is the above the same as doing a ZADD with multiple member-score values from performance perspective?


Solution

  • It won't be quite as efficient:

    • it'll send the command and key multiple times
    • there is a good chance of emptying the output buffer repeatedly, which can cause packet fragmentation

    But... it'll still be pretty fast.

    There are hacky ways around the latter, including:

    • batching
    • suspend/resume flush (be very careful to use try/finally if you do this!)

    However, you might want to know that StackExchange.Redis has a multi key/value SortedSetAdd method that does exactly what you want.