前段時(shí)間在做用戶畫像的時(shí)候,遇到了這樣的一個(gè)問(wèn)題,記錄某一個(gè)商品的用戶購(gòu)買群,剛好這種需求就可以用到Redis中的Set,key作為productID,value就是具體的customerid集合,后續(xù)的話,我就可以通過(guò)productid來(lái)查看該customerid是否買了此商品,如果購(gòu)買了,就可以有相關(guān)的關(guān)聯(lián)推薦,當(dāng)然這只是系統(tǒng)中的一個(gè)小業(yè)務(wù)條件,這時(shí)候我就可以用到SADD操作方法,代碼如下:
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.23.151:6379");
var db = redis.GetDatabase();
var productID = string.Format("productID_{0}", 1);
for (int i = 0; i 10; i++)
{
var customerID = i;
db.SetAdd(productID, customerID);
}
}
一:?jiǎn)栴}
但是上面的這段代碼很明顯存在一個(gè)大問(wèn)題,Redis本身就是基于tcp的一個(gè)Request/Response protocol模式,不信的話,可以用wireshark監(jiān)視一下:
![](/d/20211018/378db1e6cb5de3aa64fad4ac4f2fde63.gif)
從圖中可以看到,有很多次的192.168.23.1 => 192.168.23.151 之間的數(shù)據(jù)往返,從傳輸內(nèi)容中大概也可以看到有一個(gè)叫做productid_xxx的前綴,
那如果有百萬(wàn)次局域網(wǎng)這樣的round trip,那這個(gè)延遲性可想而知,肯定達(dá)不到我們預(yù)想的高性能。
二:解決方案【Batch】
剛好基于我們現(xiàn)有的業(yè)務(wù),我可以定時(shí)的將批量的productid和customerid進(jìn)行分組整合,然后用batch的形式插入到某一個(gè)具體的product的set中去,接下來(lái)我可以把上面的代碼改成類似下面這樣:
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("...:");
var db = redis.GetDatabase();
var productID = string.Format("productID_{}", );
var list = new Listint>();
for (int i = ; i ; i++)
{
list.Add(i);
}
db.SetAdd(productID, list.Select(i => (RedisValue)i).ToArray());
}
![](/d/20211018/dae9a50726408834046c86e6c63ff9d6.gif)
從截圖中傳輸?shù)膔equest,response可以看到,這次我們一次性提交過(guò)去,極大的較少了在網(wǎng)絡(luò)傳輸方面帶來(lái)的尷尬性。。
三:再次提出問(wèn)題
product維度的畫像我們可以解決了,但是我們還有一個(gè)customerid的維度,也就是說(shuō)我需要維護(hù)一個(gè)customerid為key的set集合,其中value的值為該customerid的各種平均值,比如說(shuō)“總交易次數(shù)”,“總交易金額”。。。等等這樣的聚合信息,然后推送過(guò)來(lái)的是批量的customerid,也就是說(shuō)你需要定時(shí)維護(hù)一小嘬set集合,在這種情況下某一個(gè)set的批量操作就搞不定了。。。原始代碼如下:
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("...:");
var db = redis.GetDatabase();
//批量過(guò)來(lái)的數(shù)據(jù): customeridlist, ordertotalprice,具體業(yè)務(wù)邏輯省略
var orderTotalPrice = ;
var customerIDList = new Listint>();
for (int i = ; i ; i++)
{
customerIDList.Add(i);
}
//foreach更新每個(gè)redis 的set集合
foreach (var item in customerIDList)
{
var customerID = string.Format("customerid_{}", item);
db.SetAdd(customerID, orderTotalPrice);
}
}
四:解決方案【PipeLine】
=上面這種代碼在生產(chǎn)上當(dāng)然是行不通的,不過(guò)針對(duì)這種問(wèn)題,redis早已經(jīng)提出了相關(guān)的解決方案,那就是pipeline機(jī)制,原理還是一樣,將命令集整合起來(lái)通過(guò)一條request請(qǐng)求一起送過(guò)去,由redis內(nèi)部fake出一個(gè)client做批量執(zhí)行操作,代碼如下:
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("...:");
var db = redis.GetDatabase();
//批量過(guò)來(lái)的數(shù)據(jù): customeridlist, ordertotalprice,具體業(yè)務(wù)邏輯省略
var orderTotalPrice = ;
var customerIDList = new Listint>();
for (int i = ; i ; i++)
{
customerIDList.Add(i);
}
var batch = db.CreateBatch();
foreach (var item in customerIDList)
{
var customerID = string.Format("customerid_{}", item);
batch.SetAddAsync(customerID, orderTotalPrice);
}
batch.Execute();
}
然后,我們?cè)倏聪旅娴膚ireshark截圖,可以看到有很多的SADD這樣的小命令,這就說(shuō)明有很多命令是一起過(guò)去的,大大的提升了性能。
![](/d/20211018/2a2abb5aef21b6d17866d215b6723811.gif)
最后可以再看一下redis,數(shù)據(jù)也是有的,是不是很爽~~~
192.168.23.151:6379> keys *
1) "customerid_0"
2) "customerid_9"
3) "customerid_1"
4) "customerid_3"
5) "customerid_8"
6) "customerid_2"
7) "customerid_7"
8) "customerid_5"
9) "customerid_6"
10) "customerid_4"
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Redis cluster集群的介紹
- Spring-data-redis操作redis cluster的示例代碼
- Windows環(huán)境下Redis Cluster環(huán)境搭建(圖文)
- 如何用docker部署redis cluster的方法
- 在Redis集群中使用pipeline批量插入的實(shí)現(xiàn)方法
- python使用pipeline批量讀寫redis的方法
- 詳解Java使用Pipeline對(duì)Redis批量讀寫(hmset&hgetall)
- redis cluster支持pipeline的實(shí)現(xiàn)思路