Redis性能大幅提升之Batch批量讀寫詳解
前言
本文主要介紹的是關(guān)于Redis性能提升之Batch批量讀寫的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來看看詳細(xì)的介紹:
提示:本文針對的是StackExchange.Redis
一、問題呈現(xiàn)
前段時(shí)間在開發(fā)的時(shí)候,遇到了redis批量讀的問題,由于在StackExchange.Redis里面我確實(shí)沒有找到PipeLine命令,找到的是Batch命令,因此對其用法進(jìn)行了探究一下。
下面的代碼是我之前寫的:
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;
}
從上面的代碼中可以看出,并不是批量讀,經(jīng)過性能測試,性能確實(shí)是要遠(yuǎn)遠(yuǎn)低于用Batch操作,因?yàn)镠ashGetAll方法被執(zhí)行了多次。
下面給出批量方法:
二、解決問題方法
具體的用法是:
var batch = db.CreateBatch(); ...//這里寫具體批量操作的方法 batch.Execute();
2.1批量寫:
具體代碼:
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;
}
這個(gè)方法里執(zhí)行的是批量插入學(xué)生實(shí)體數(shù)據(jù),這里只是針對Hash,其它的也一樣操作。
2.2批量讀:
具體代碼:
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;
}
這個(gè)方法是批量讀取學(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 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));
...
}
}
這個(gè)方法里也給出了批量寫和讀的操作。
總結(jié)
好了,先說到這里了。以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
redis分布式Jedis類型轉(zhuǎn)換的異常深入研究
這篇文章主要介紹了redis分布式Jedis類型轉(zhuǎn)換的異常深入研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Redis中主鍵失效的原理及實(shí)現(xiàn)機(jī)制剖析
這篇文章主要介紹了Redis中主鍵失效的原理及實(shí)現(xiàn)機(jī)制剖析,本文講解了失效時(shí)間的控制、失效的內(nèi)部實(shí)現(xiàn)、Memcached 刪除失效主鍵的方法與 Redis 有何異同、Redis 的主鍵失效機(jī)制會不會影響系統(tǒng)性能等內(nèi)容,需要的朋友可以參考下2015-06-06
Redis+AOP+自定義注解實(shí)現(xiàn)限流
這篇文章主要為大家詳細(xì)介紹了如何利用Redis+AOP+自定義注解實(shí)現(xiàn)個(gè)小功能:自定義攔截器限制訪問次數(shù),也就是限流,感興趣的可以了解一下2022-06-06
基于Redis實(shí)現(xiàn)抽獎(jiǎng)功能及問題小結(jié)
這篇文章主要介紹了基于Redis實(shí)現(xiàn)抽獎(jiǎng)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Redis的setNX分布式鎖超時(shí)時(shí)間失效 -1問題及解決
這篇文章主要介紹了Redis的setNX分布式鎖超時(shí)時(shí)間失效 -1問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Redis Sentinel實(shí)現(xiàn)哨兵模式搭建小結(jié)
這篇文章主要介紹了Redis Sentinel實(shí)現(xiàn)哨兵模式搭建小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12

