濮阳杆衣贸易有限公司

主頁 > 知識庫 > Redis性能大幅提升之Batch批量讀寫詳解

Redis性能大幅提升之Batch批量讀寫詳解

熱門標(biāo)簽:西藏教育智能外呼系統(tǒng)價格 地圖標(biāo)注費(fèi)用 地圖標(biāo)注如何即時生效 百度商家地圖標(biāo)注怎么做 小紅書怎么地圖標(biāo)注店 太原營銷外呼系統(tǒng) 最簡單的百度地圖標(biāo)注 竹間科技AI電銷機(jī)器人 玄武湖地圖標(biāo)注

前言

本文主要介紹的是關(guān)于Redis性能提升之Batch批量讀寫的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來看看詳細(xì)的介紹:

提示:本文針對的是StackExchange.Redis

一、問題呈現(xiàn)

前段時間在開發(fā)的時候,遇到了redis批量讀的問題,由于在StackExchange.Redis里面我確實(shí)沒有找到PipeLine命令,找到的是Batch命令,因此對其用法進(jìn)行了探究一下。

下面的代碼是我之前寫的:

public ListStudentEntity> Get(Listint> ids)
{
  ListStudentEntity> result = new ListStudentEntity>();
  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;
}

從上面的代碼中可以看出,并不是批量讀,經(jīng)過性能測試,性能確實(shí)是要遠(yuǎn)遠(yuǎn)低于用Batch操作,因?yàn)镠ashGetAll方法被執(zhí)行了多次。

下面給出批量方法:

二、解決問題方法

具體的用法是:

var batch = db.CreateBatch();

...//這里寫具體批量操作的方法

batch.Execute();

2.1批量寫:

具體代碼:

public bool InsertBatch(ListStudentEntity> 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;
}

這個方法里執(zhí)行的是批量插入學(xué)生實(shí)體數(shù)據(jù),這里只是針對Hash,其它的也一樣操作。 

2.2批量讀:

具體代碼:

public ListStudentEntity> GetBatch(Listint> ids)
{
  ListStudentEntity> result = new ListStudentEntity>();
  ListTaskStackExchange.Redis.HashEntry[]>> valueList = new ListTaskStackExchange.Redis.HashEntry[]>>();
  try
  {
   var db = RedisCluster.conn.GetDatabase();
   var batch = db.CreateBatch();
   foreach(int id in ids)
   {
    string key = KeyManager.GetKey(id);
    TaskStackExchange.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;
}

這個方法是批量讀取學(xué)生實(shí)體數(shù)據(jù),批量拿到實(shí)體數(shù)據(jù)后,將其轉(zhuǎn)化成我們需要的數(shù)據(jù)。下面給出性能對比。

2.3性能對比:

10條數(shù)據(jù),約4-5倍差距:

   

1000條數(shù)據(jù),約28倍的差距:

 

隨著數(shù)據(jù)了增多,差距將越來越大。

三、源碼測試案例 

上面是批量讀寫實(shí)體數(shù)據(jù),下面給出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 ListTask>();
    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));
    ...
   }
  }

這個方法里也給出了批量寫和讀的操作。

總結(jié)

好了,先說到這里了。以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • 深入了解Redis的性能
  • asp.net性能優(yōu)化之使用Redis緩存(入門)
  • 關(guān)于redis狀態(tài)監(jiān)控和性能調(diào)優(yōu)詳解

標(biāo)簽:林芝 廣東 唐山 揚(yáng)州 澳門 香港 景德鎮(zhèn) 贛州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis性能大幅提升之Batch批量讀寫詳解》,本文關(guān)鍵詞  Redis,性能,大幅,提升,之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Redis性能大幅提升之Batch批量讀寫詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于Redis性能大幅提升之Batch批量讀寫詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    昭通市| 句容市| 邯郸县| 抚顺县| 友谊县| 沂水县| 镇安县| 清水县| 镇平县| 广昌县| 石狮市| 利辛县| 饶平县| 扎鲁特旗| 长垣县| 沁水县| 文化| 登封市| 夏邑县| 绥江县| 苍溪县| 芦溪县| 白山市| 横峰县| 榆中县| 普兰县| 新乡市| 石渠县| 白玉县| 博罗县| 北辰区| 嘉禾县| 弥勒县| 蒙自县| 临西县| 海原县| 凤庆县| 盐山县| 禹州市| 特克斯县| 平利县|