Cómo trabajar con valores numéricos/enteros en Redis con Booksleeve

I'm currently using an in-memory cache and looking to switch to a distributed caching mechanism with Redis. I had a look at ServiceStack's client, but the rate-limiting licensing doesn't work for me, so Booksleeve seems to be recommended.

I've set up a test program to just set and that get that same value back from Booksleeve, but it seems like the result I'm getting back isn't in my expected format, and I'm not sure what the right way to handle this is (there isn't much in the way of documentation that I can see). Here is my simple test program:

            RedisConnection conn = new RedisConnection("localhost", 6379);
            conn.Strings.Set(1, "test", 100);
            var task = conn.Strings.Get(1, "test");
            var x = task.Result; // byte[] result = {49, 48, 48};
            var str = BitConverter.ToString(x); // string result = "31-30-30";
            var l = BitConverter.ToInt64(x, 0); // Destination array is not long enough to copy all the items in the collection. Check array index and length.

As you can see, at no point do I get back the same value of "100" that I cached with my original "Set" statement. It's also interesting that I don't seem to be able to cache by numeric values (since Get and Set are members of conn.Strings). Is the recommended approach to just .ToString() all numeric key values?

Any explanation as to why I'm unable to get back the original cached value (and best practices for doing this) would be greatly appreciated.

preguntado el 05 de febrero de 14 a las 17:02

1 Respuestas

My answer has two parts:

  1. Redis always saves strings, even if you set a number. BUT it internally knows to do some specific actions on strings that represent números. For example, if right after your first .Set() assignment you'll add:

    conn.Strings.Increment(1, "test", 1);

    al test key will have the value "101", which is a string, but one that is made out of an arithmetic calculation by Redis.

  2. You need to fix your conversion function. Instead of using BitConverter, that's the right way to convert:

    var str = System.Text.Encoding.UTF8.GetString(x);
    var value = int.Parse(str);

    Of course, this snippet doesn't include any kind of error checking, which is fairly easy to apply (e.g. what if the value is empty or contains something that is not a number).

As for your last question, is using .ToString() the recommended approach - yes. That's the way to work with Redis. But of course, you can make your own utility wrappers that take care of converting values that suppose to contian numbers, to numbers. Something like GetIntValue(string key) oro por lo tanto.

Respondido 06 Feb 14, 03:02

Awesome, thanks for the thorough answer. I figured redis treated everything as strings, but was a bit put off by the numeric operations. Thanks for clearing that up - Chris

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.