服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - Redis性能大幅提升之Batch批量读写详解

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

2019-11-06 12:41649727360 Redis

这篇文章主要给大家介绍了关于Redis性能大幅提升之Batch批量读写的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来跟着小编一起来学习学习吧。

前言

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

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

一、问题呈现

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public List<StudentEntity> Get(List<int> ids)
{
  List<StudentEntity> result = new List<StudentEntity>();
  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方法被执行了多次。

下面给出批量方法:

二、解决问题方法

具体的用法是:

?
1
2
3
4
5
var batch = db.CreateBatch();
 
...//这里写具体批量操作的方法
 
batch.Execute();

2.1批量写:

具体代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public bool InsertBatch(List<StudentEntity> seList)
{
  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批量读:

具体代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public List<StudentEntity> GetBatch(List<int> ids)
{
  List<StudentEntity> result = new List<StudentEntity>();
  List<Task<StackExchange.Redis.HashEntry[]>> valueList = new List<Task<StackExchange.Redis.HashEntry[]>>();
  try
  {
   var db = RedisCluster.conn.GetDatabase();
   var batch = db.CreateBatch();
   foreach(int id in ids)
   {
    string key = KeyManager.GetKey(id);
    Task<StackExchange.Redis.HashEntry[]> tres = 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倍差距:

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

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

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

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

三、源码测试案例

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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));
    ...
   }
  }

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

总结

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

延伸 · 阅读

精彩推荐
  • RedisRedis存取序列化与反序列化性能问题详解

    Redis存取序列化与反序列化性能问题详解

    这篇文章主要给大家介绍了关于Redis存取序列化与反序列化性能问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    这名字已经存在9742021-02-24
  • Redis在ssm项目中使用redis缓存查询数据的方法

    在ssm项目中使用redis缓存查询数据的方法

    本文主要简单的使用Java代码进行redis缓存,即在查询的时候先在service层从redis缓存中获取数据。如果大家对在ssm项目中使用redis缓存查询数据的相关知识感...

    caychen8962019-11-12
  • RedisRedis分布式锁升级版RedLock及SpringBoot实现方法

    Redis分布式锁升级版RedLock及SpringBoot实现方法

    这篇文章主要介绍了Redis分布式锁升级版RedLock及SpringBoot实现,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以...

    等不到的口琴7802021-07-25
  • Redis就这?Redis持久化策略——AOF

    就这?Redis持久化策略——AOF

    今天为大家介绍Redis的另一种持久化策略——AOF。注意:AOF文件只会记录Redis的写操作命令,因为读命令对数据的恢复没有任何意义...

    头发茂密的刘叔4052021-12-14
  • RedisLinux Redis 的安装步骤详解

    Linux Redis 的安装步骤详解

    这篇文章主要介绍了 Linux Redis 的安装步骤详解的相关资料,希望大家通过本文能掌握如何安装Redis,需要的朋友可以参考下 ...

    carl-zhao3822019-11-08
  • RedisRedis数据结构之链表与字典的使用

    Redis数据结构之链表与字典的使用

    这篇文章主要介绍了Redis数据结构之链表与字典的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    白泽来了4052021-08-03
  • Redisredis启动,停止,及端口占用处理方法

    redis启动,停止,及端口占用处理方法

    今天小编就为大家分享一篇redis启动,停止,及端口占用处理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    澄海单挑狂5152019-11-14
  • Redis聊一聊Redis与MySQL双写一致性如何保证

    聊一聊Redis与MySQL双写一致性如何保证

    一致性就是数据保持一致,在分布式系统中,可以理解为多个节点中数据的值是一致的。本文给大家分享Redis与MySQL双写一致性该如何保证,感兴趣的朋友一...

    mind_programmonkey6432021-08-12