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

本文介绍了如何利用StackExchange.Redis库的管道和批量操作实现高性能的HashSet数据插入。通过使用HashEntry[] hashFields方法的HMSET指令进行批量插入,以及CreateBatch方法创建管道,结合batch.Execute()执行,可以显著提高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<Task>();
                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<Task>();
                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)操作

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值