侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Redis性能大幅提升之Batch批量读写详解

Redis  /  管理员 发布于 5年前   1133


前言

本文主要介绍的是关于Redis性能提升之Batch批量读写的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:

提示:本文针对的是StackExchange.Redis

一、问题呈现

前段时间在开发的时候,遇到了redis批量读的问题,由于在StackExchange.Redis里面我确实没有找到PipeLine命令,找到的是Batch命令,因此对其用法进行了探究一下。

下面的代码是我之前写的:

public ListGet(Listids){  
Listresult = new List();  
try  {   
var db = RedisCluster.conn.GetDatabase();   
foreach (int id in ids.Keys)   {    
string key = KeyManager.GetKey(id);    
var dic = db.HashGetAll(key).ToDictionary(k => k.Name, v => v.Value);    

StudentEntity se = new StudentEntity(); 
   
if (dic.Keys.Contains(StudentEntityRedisHashKey.id.ToString()))    {    
 pe.id = FormatUtils.ConvertToInt32(dic[StudentEntityRedisHashKey.id.ToString()], -1);    }    if (dic.Keys.Contains(StudentEntityRedisHashKey.name.ToString()))    {     pe.name= dic[StudentEntityRedisHashKey.name.ToString()];    }    result.Add(se);   }   catch (Exception ex)   {   }   return result;}

从上面的代码中可以看出,并不是批量读,经过性能测试,性能确实是要远远低于用Batch操作,因为HashGetAll方法被执行了多次。

下面给出批量方法:

二、解决问题方法

具体的用法是:

var batch = db.CreateBatch();...//这里写具体批量操作的方法batch.Execute();

2.1批量写:

具体代码:

public bool InsertBatch(ListseList){  
bool result = false;  
try  {   
var db = RedisCluster.conn.GetDatabase();   
var batch = db.CreateBatch();   
foreach (var se in seList)   {    
string key = KeyManager.GetKey(se.id);    
batch.HashSetAsync(key, StudentEntityRedisHashKey.id.ToString(), te.id);    
batch.HashSetAsync(key, StudentEntityRedisHashKey.name.ToString(), te.name);   
}  
 batch.Execute();   
 result = true;  
 }  catch (Exception ex)  {  
 }  
 return result;
 }

这个方法里执行的是批量插入学生实体数据,这里只是针对Hash,其它的也一样操作。 

2.2批量读:

具体代码:

public ListGetBatch(Listids){  
Listresult = new List();  
List<Task> valueList = new List<Task>();  
try  {   
var db = RedisCluster.conn.GetDatabase();   
var batch = db.CreateBatch();   
foreach(int id in ids)   {    
string key = KeyManager.GetKey(id);    
Tasktres = batch.HashGetAllAsync(key);    
valueList.Add(tres);   
}   
batch.Execute();   
foreach(var hashEntry in valueList)   {    
var dic = hashEntry.Result.ToDictionary(k => k.Name, v => v.Value);    
StudentEntity se= new StudentEntity();    
if (dic.Keys.Contains(StudentEntityRedisHashKey.id.ToString()))    {     
se.id= FormatUtils.ConvertToInt32(dic[StudentEntityRedisHashKey.id.ToString()], -1);    
}    
if (dic.Keys.Contains(StudentEntityRedisHashKey.name.ToString()))    {     
se.name= dic[StudentEntityRedisHashKey.name.ToString()];    
}    
result.Add(se);  
 }  
 }  catch (Exception ex)  {  
 }  
 return result;
 }

这个方法是批量读取学生实体数据,批量拿到实体数据后,将其转化成我们需要的数据。下面给出性能对比。

2.3性能对比:

10条数据,约4-5倍差距:

   

1000条数据,约28倍的差距:

 

随着数据了增多,差距将越来越大。

三、源码测试案例 

上面是批量读写实体数据,下面给出StackExchange.Redis源码测试案例里的批量读写写法:

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));    
...  
 }  
 }

这个方法里也给出了批量写和读的操作。

总结

好了,先说到这里了。以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家的支持。



  • 上一条:
    redis快照模式
    下一条:
    golang实现php里的serialize()和unserialize()序列和反序列方法详解
  • 昵称:

    邮箱:

    2条评论 (评论内容有缓存机制,请悉知!)
    最新最热

    @ Lucio 

    Thank you very much for your love. I am very happy. This site mainly displays the technical articles I use in my work. I will also write some tutorials on the Internet. Thank you again.



    博主  2021-06-17 14:42:23 赞 (0)

    This is very interesting, You're a very skilled blogger. I've joined your feed and look forward to seeking more of your excellent post. Also, I have shared your website in my social networks! สมัคร Wm Casino

    Lucio  2021-06-16 22:52:53 赞 (0)
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Redis中能实现的功能、常见应用介绍(0个评论)
    • 2024年Redis面试题之一(0个评论)
    • 在redis缓存常见出错及解决方案(0个评论)
    • 在redis中三种特殊数据类型:地理位置、基数(cardinality)估计、位图(Bitmap)使用场景介绍浅析(2个评论)
    • Redis 删除 key用 del 和 unlink 有啥区别?(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    • 2022-08
    • 2023-02
    • 2023-04
    • 2023-07
    • 2024-01
    • 2024-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客