StackExchange.Redis 管道 批量 高性能插入数据

现在用redis来做数据缓存的越来越多了,很多项目都有初始化redis数据的过程,由于初始化的数据比较大,那么该过程越快越好。这里我们以HashSet方法为例,


这里我们推荐用HashEntry[] hashFields方法传入多个fields,应为它发送的HMSET指令即批量插入数据,另一个方法发送的HSET指令。

在阅读StackExchange.Redis里面我确实没有找到pipe指令,后来发现该指令的实现是:通过CreateBatch方法实现的。源码的单元测试例子是:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Tests
{
    [TestFixture]
    public class Batches
    {
        [Test]
        public void TestBatchNotSent()
        {
            using (var muxer = Config.GetUnsecuredConnection())
            {
                var conn = muxer.GetDatabase(0);
                conn.KeyDeleteAsync("batch");
                conn.StringSetAsync("batch", "batch-not-sent");
                var tasks = new List();
                var batch = conn.CreateBatch();
                
                tasks.Add(batch.KeyDeleteAsync("batch"));
                tasks.Add(batch.SetAddAsync("batch", "a"));
                tasks.Add(batch.SetAddAsync("batch", "b"));
                tasks.Add(batch.SetAddAsync("batch", "c"));

                Assert.AreEqual("batch-not-sent", (string)conn.StringGet("batch"));
            }
        }

        [Test]
        public void TestBatchSent()
        {
            using (var muxer = Config.GetUnsecuredConnection())
            {
                var conn = muxer.GetDatabase(0);
                conn.KeyDeleteAsync("batch");
                conn.StringSetAsync("batch", "batch-sent");
                var tasks = new List();
                var batch = conn.CreateBatch();
                tasks.Add(batch.KeyDeleteAsync("batch"));
                tasks.Add(batch.SetAddAsync("batch", "a"));
                tasks.Add(batch.SetAddAsync("batch", "b"));
                tasks.Add(batch.SetAddAsync("batch", "c"));
                batch.Execute();
                
                var result = conn.SetMembersAsync("batch");
                tasks.Add(result);
                Task.WhenAll(tasks.ToArray());

                var arr = result.Result;
                Array.Sort(arr, (x, y) => string.Compare(x, y));
                Assert.AreEqual(3, arr.Length);
                Assert.AreEqual("a", (string)arr[0]);
                Assert.AreEqual("b", (string)arr[1]);
                Assert.AreEqual("c", (string)arr[2]);
            }
        }
    }
}

   var batch = conn.CreateBatch();这里的batch实际就是管道。真正的执行需要调用 batch.Execute()方法。网上也有类似的文章 redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作

 

你可能感兴趣的:(Memcached,redis)