1

I have the CLI open and a .NET dummy solution open. I would like to create a user and insert some data in one go. How in the CLI/REPL i wrote this

redis 127.0.0.1:6379> incr userCount
(integer) 1
redis 127.0.0.1:6379> set user:1:name acid
OK
redis 127.0.0.1:6379> set user:1:pw zombie
OK

Thats all fine but how do i do this in one go? It would be nice if i could get the userid at the end of the operation but really i don't need it for this example and i'd like to pipeline the 3 commands at once. How do i do that? Also maybe i should be doing it in a transaction? Also i probably should check if the name exist before inserting but I havent gotten that far yet.

using BookSleeve;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedisEx
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var conn = new RedisConnection("localhost"))
            {
                conn.Open();
                var userC = conn.Strings.Increment(1, "userCount");
                var userid = conn.Wait(userC);
                if (userid > 1)
                {
                    var sz = conn.Wait(conn.Strings.GetString(1, string.Format("user:{0}:name", userid - 1)));
                    Console.WriteLine(sz);
                }
                conn.Strings.Set(1, string.Format("user:{0}:name", userid), "acid");
                conn.Strings.Set(1, string.Format("user:{0}:pw", userid), "zombie");
            }
        }
    }
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Q: Do you want to write a script that invokes Redis-CLI client commands, a Redis-Booksleeve client, or "something else"? – paulsm4 Aug 30 '12 at 23:23
  • @paulsm4: i'd be writing code in .NET. But if someone tells me the CLI command i may be able to guess/look it up using .NET/booksleeve –  Aug 30 '12 at 23:29

1 Answers1

1

Please look at this C# example:

It illustrates adding "shippers" records to a Redis "list". Just substitute "user" for "shipper", and lose the "incr". Your "count" simply becomes the #/items in the list :)

The example uses ServiceStack instead of Booksleeve, but the principles should be exactly the same.

'Hope that helps .. PSM

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • `Count becomes number of items in list` is wrong. ServiceStack actually creates a key behind the scenes and runs the `incr` command on it whenever `GetNextSequence` is called. Using number of items in the list to generate sequence numbers has race problems – Sripathi Krishnan Aug 31 '12 at 03:32