StackExchange.Redis简单的C#示例

hel*_*hab 20 c# redis stackexchange.redis

我正在寻找一个非常简单的启动C#应用程序来使用StackExchange.Redis我在网上搜索并找到了StackExchange.Redis

但这似乎不是一个快速启动的例子.

我使用StackExchange.Redis exe在Windows上设置了redis

任何人都可以帮我找到一个简单的C#应用​​程序连接redis服务器并设置和获取一些密钥.

the*_*000 21

您可以在自述文件中找到C#示例.

using StackExchange.Redis;
...
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase db = redis.GetDatabase();
string value = "abcdefg";
db.StringSet("mykey", value);
...
string value = db.StringGet("mykey");
Console.WriteLine(value); // writes: "abcdefg"
Run Code Online (Sandbox Code Playgroud)


Kam*_*hid 9

从他们的github示例中查看以下代码:

 using (var muxer = ConnectionMultiplexer.Connect("localhost,resolvedns=1"))
        {
            muxer.PreserveAsyncOrder = preserveOrder;
            RedisKey key = "MBOA";
            var conn = muxer.GetDatabase();
            muxer.Wait(conn.PingAsync());

            Action<Task> nonTrivial = delegate
            {
                Thread.SpinWait(5);
            };
            var watch = Stopwatch.StartNew();
            for (int i = 0; i <= AsyncOpsQty; i++)
            {
                var t = conn.StringSetAsync(key, i);
                if (withContinuation) t.ContinueWith(nonTrivial);
            }
            int val = (int)muxer.Wait(conn.StringGetAsync(key));
            watch.Stop();

            Console.WriteLine("After {0}: {1}", AsyncOpsQty, val);
            Console.WriteLine("({3}, {4})\r\n{2}: Time for {0} ops: {1}ms; ops/s: {5}", AsyncOpsQty, watch.ElapsedMilliseconds, Me(),
                withContinuation ? "with continuation" : "no continuation", preserveOrder ? "preserve order" : "any order",
                AsyncOpsQty / watch.Elapsed.TotalSeconds);
        }
Run Code Online (Sandbox Code Playgroud)