1. 簡介
10多年的渝中網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整渝中建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“渝中網(wǎng)站設(shè)計”,“渝中網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。現(xiàn)在計算機用二進制(位)作為信息的基礎(chǔ)單位,1個字節(jié)等于8位,例如 "abc" 字符串是由3個字節(jié)組成,單實際在計算機存儲時將其用二進制表示, "abc" 分別對應(yīng)的 ASCII 碼分別是97、98、99,對應(yīng)的二進制分別是01100001、01100010和01100011,如下
a >>>>01100001
b >>>>01100010 >>>>abc >>>>011000010110001001100011
c >>>>01100011
合理地使用操作位能夠有效的提高內(nèi)存使用率和開發(fā)效率。
Redis 提供了 Bitmaps 這個“數(shù)據(jù)類型”可以實現(xiàn)對位的操作:
? (1) Bitmaps 本身不是一種數(shù)據(jù)類型,實際上它就是字符串(key-value),但是它可以對字符串的位進行操作。
? (2) Bitmaps 單獨提供了一套命令,所以在 Redis 中使用 Bitmaps 和使用字符串的方法不太相同??梢园?Bitmaps 想象成一個以位為單位的數(shù)組,數(shù)組的每個單元智能存儲0和1,數(shù)組的下標在 Bitmaps 中叫做偏移量。
key >>>>value 011000010110001001100011
2. 命令
setbit
(1)格式
setbit
127.0.0.1:6379>setbit
* offset :偏移量從0開始
(2)實例
? 每個獨立用戶是否訪問過網(wǎng)站存放在 Bitmaps 中,將訪問的用戶記做1,沒有訪問的用戶自作0,用偏移量記做用戶的 id 。
? 設(shè)置鍵的第 offset 個位的值(從0算起),假設(shè)現(xiàn)在有20個用戶, userid =1,6,11,15,19的用戶對網(wǎng)站進行了訪問,那么當前 Bitmaps 初始化結(jié)果如圖
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
↓ | ↓ | ↓ | ↓ | ↓ | |||||||||||||||
0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
unique:users:20201106 代表2020-11-06這天的獨立訪問用戶的 Bitmaps
127.0.0.1:6379>setbit unique:users:20201106 1 1
(integer) 0
127.0.0.1:6379>setbit unique:users:20201106 6 1
(integer) 0
127.0.0.1:6379>setbit unique:users:20201106 11 1
(integer) 0
127.0.0.1:6379>setbit unique:users:20201106 15 1
(integer) 0
127.0.0.1:6379>setbit unique:users:20201106 19 1
(integer) 0
注:
? 很多應(yīng)用的用戶 id 以一個指定數(shù)字(例如10000)開頭,直接將用戶 id 和 Bitmaps 的偏移量對應(yīng)勢必會造成一定的浪費,通常的做法是每次做 setbit 操作時將用戶 id 減去這個指定數(shù)字。
? 在第一次初始化 Bitmaps 時,假如偏移量非常大,那么整個初始化過程執(zhí)行會比較慢,可能會造成 Redis 的阻塞。
getbit
(1)格式
getbit
127.0.0.1:6379>getbit
獲取鍵的第 offset 位的值(從0開始算)
(2)實例
獲取 id =8的用戶是否在2020-11-06這天訪問過,返回0說明沒訪問過:
127.0.0.1:6379>getbit unique:users:20201106 8
(integer) 0
127.0.0.1:6379>getbit unique:users:20201106 1
(integer) 1
127.0.0.1:6379>getbit unique:users:20201106 100
(integer) 0
注:因為100根本不存在,所以也是返回0
bitcount
統(tǒng)計字符串被設(shè)置為1的 bit 數(shù)。一般情況下,給定的整個字符串都會被進行計數(shù),通過指定額外的 start 或 end 參數(shù),可以讓計數(shù)只在特定的位上進行。 start 和 end 參數(shù)的設(shè)置,都可以使用負數(shù)值:比如-1表示最后一個位,而-2表示倒數(shù)第二個位, start 、 end 是指 bit 組的字節(jié)的下標數(shù),二者皆包含。
(1)格式
bitcount
127.0.0.1:6379>bitcount
(2)實例
計算2020-11-06這天的獨立訪問用戶數(shù)量
127.0.0.1:6379>bitcount unique:users:20201106
(integer) 5
start?和?end?代表起始和結(jié)束字節(jié)數(shù),下面操作計算用戶 id 在第1個字節(jié)到第3個字節(jié)之間的獨立訪問用戶數(shù)量,對應(yīng)的用戶 id 是11,15,19。
127.0.0.1:6379>bitcount unique:users:20201106 1 3
(integer) 3
舉例:K1【01000001 01000000 00000000 00100001】,對應(yīng)【0,1,2,3】
bitcount K1 1 2? :統(tǒng)計下標1、2字節(jié)組中 bit =1的個數(shù),即01000000 00000000 >>>>1
bitcount K1 1 3? :統(tǒng)計下標1、3字節(jié)組中 bit =1的個數(shù),即01000000 00100001?>>>>3
bitcount K1 1 -2? :統(tǒng)計下標1、3字節(jié)組中 bit =1的個數(shù),即01000000 00000000 >>>>3
注意: redis 的 setbit 設(shè)置或清除的是 bit 位置,而 bitcount 計算的是 byte 位置。
bitop
(1)格式
bitop and (or/not/xor)
127.0.0.1:6379>bittop
bitop 是一個復(fù)合操作,他可以做多個 Bitmaps 的 and (交集)、 or (并集)、 not (非)、xor (異或)操作并將結(jié)果保存在 destkey中。
(2)實例
2020-11-04日訪問網(wǎng)站的 userid =1,2,5,9
setbit unique:users:20201104 1 1
setbit unique:users:20201104 2?1
setbit unique:users:20201104 5?1
setbit unique:users:20201104 9?1
2020-11-03日訪問網(wǎng)站的 userid = 0,1,4,9
setbit unique:users:20201103?0?1
setbit unique:users:20201103 1?1
setbit unique:users:20201103 4?1
setbit unique:users:20201103 9?1
計算出兩天都訪問過網(wǎng)站的用戶數(shù)量
bitop and unique:users:and:20201104_03
unique:users:20201103 unique:users:20201104
127.0.0.1:6379>bitop and unique:users:and:20201104_03 unique:users:20201103 unique:users:20201104
(integer) 2
127.0.0.1:6379>bitcount unique:users:and:20201104_03
↓ | ↓ | ↓ | ↓ | |||||||
unique:users:20201104 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
?????????????????????????????↓
↓ | ↓ | ↓ | ↓ | |||||||
unique:users:20201103 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
? ↓
↓ | ↓ | |||||||||
unique:users:20201104_03 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
計算出任意一天都訪問過網(wǎng)站的用戶數(shù)量(例如月活躍就是類似這種),可以使用 or 求并集
127.0.0.1:6379>bitop or unique:users:or:20201104_03 unique:users:20201103 unique:users:20201104
(integer) 2
127.0.0.1:6379>bitcount unique:users:or:20201104_03
(integer) 6
3. Bitmaps 與 set 對比
? 假設(shè)網(wǎng)站有1億用戶,每天獨立訪問的用戶有5千萬,如果每天用集合類型和 Bitmaps 分別存儲活躍用戶可以得到表
數(shù)據(jù)類型 | 每個用戶 id 占用空間 | 需要存儲的用戶量 | 全部內(nèi)存量 |
集合類型 | 64位 | 50000000 | 64位*50000000=400MB |
Bitmaps | 1位 | 100000000 | 1位*100000000=12.5MB |
? 很明顯,這種情況下使用 Bitmaps 能節(jié)省很多的內(nèi)存空間,尤其是隨著時間推移節(jié)省的內(nèi)存還是非??捎^的
數(shù)據(jù)類型 | 一天 | 一個月 | 一年 |
集合類型 | 400MB | 12GB | 144GB |
Bitmaps | 12.5MB | 375MB | 4.5GB |
? 但 Bitmaps 并不是萬金油,假如該網(wǎng)站每天的嘟嚕訪問用戶很少,例如只有10萬(大量的僵尸用戶),那么兩者的對比如下表所示,很顯然,這時候使用 Bitmaps 就不太合適了,因為基本上大部分位都是0
數(shù)據(jù)類型 | 每個用戶 id 占用空間 | 需要存儲的用戶量 | 全部內(nèi)存量 |
集合類型 | 64位 | 100000 | 64位*100000=800KB |
Bitmaps | 1位 | 100000000 | 1位*100000000=12.5MB |
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當前標題:Bitmaps-創(chuàng)新互聯(lián)
URL標題:http://www.rwnh.cn/article26/dspsjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名、云服務(wù)器、移動網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容