当前位置:网站首页>Bitmap of redis data structure
Bitmap of redis data structure
2022-06-24 14:41:00 【Sword Saint without trace】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 29 God , Click to see the event details
background
Project development process , We often use boolean Type to store data . For example, record the daily check-in of users , Check in yes 1, Otherwise 0, If we need to count the number of sign ins in a year , If the String To store , Every user needs to record 365 Time , When the number of users is very large , The storage space required is huge . To solve this problem ,Redis Bitmap data structure is provided to solve this problem .
brief introduction
bitmap Bitmap for short , Is an array of multiple binary bits , Each binary bit in the array has its corresponding offset , These offsets can be used to manipulate one or more binary bits specified in the bitmap .
data structure
explain : A bitmap can also be viewed as an array of bytes , The index sequence number represents the corresponding value ,bitmap The default value is zero 0
Basic commands
setbit Set the value of the binary bit
Basic grammar
setbit key offset value
Be careful : Set up bit When the value of , among offset The value of cannot be negative , Otherwise it will be reported ERR bit offset is not integer or out of range error .
example
Place bitmap mbit Set to :10010100
# Set the first position to 1
setbit mbit 0 1
# Set the fourth position to 1
setbit mbit 3 1
# Set the sixth position to 1
setbit mbit 5 1
The changing process of bitmap
Bitmap extension
When the user executes setbit On command , If the bitmap does not exist , Or the current size of the bitmap cannot meet the setting operation that the user wants to perform , that Redis The set bitmap will be extended , So that the bitmap can meet the user's setting request .
For example, the user executes the following command :
setbit mbit 10 1
Redis The bitmap created is not just 11 Binary bits , Instead, there are two bytes in total 16 Binary bits , As shown in the figure below .
explain : We can also see from the picture , Except that the offset is 10 Outside the binary bit of , All other bits that are not set are initialized to 0.
getbit Get binary value
Basic grammar
getbit key offset
Example
bitcount Count the number of binary bits set
Basic grammar
bitcount key start end
Example
bitop For one or more strings that hold binary bits key Perform bit operation
Basic grammar
bitop and key1 key2
explain : bitop Command support and、or、not、xor These four operations
- and: And operators (&) Two at the same time 1, The result is 1, Otherwise 0
- or: Or operations (|) One for 1, Its value is 1
- not: Take the opposite (0110 0001 NOT: 1001 1110)
- xor: Exclusive or operation , Values differ by 1, Otherwise 0
Example
bitpos Used to find the first... In the specified range 0 or 1 ( Bytes are units )
Basic grammar
bitpos key start end
Example
Application scenarios
- 1. User sign in times
1 Representatives sign in ,0 The representative didn't sign in , In this way, you can easily count the activity of users . Compared to using strings directly , Each record in the bitmap occupies only one bit position , This greatly reduces the memory space utilization .
2. Count login times
summary
This article explains the basic data structure and operation commands of bitmap , Bitmap is suitable for some specific scenes , We need to gather the actual business scenarios , Choosing the right data structure storage can greatly reduce Redis Of memory space .
边栏推荐
- Successfully solved: selenium common. exceptions. SessionNotCreatedException: Message: session not created: This versi
- box-sizing
- update+catroot+c000021a+critical service failed+drivers+intelide+viaide+000000f
- [learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
- Keras深度学习实战(11)——可视化神经网络中间层输出
- Virtual machines on the same distributed port group but different hosts cannot communicate with each other
- 10 Ces autographes très stylisés.
- laravel8使用faker调用工厂填充数据
- leetcode.12 --- 整数转罗马数字
- 安装wireshark时npcap怎么都安装不成功,建议先用winpcap
猜你喜欢

leetcode. 12 --- integer to Roman numeral

Keyword of ES mapping; Term query add keyword query; Change mapping keyword type

C language ---18 function (user-defined function)

ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型

Qunhui synchronizes with alicloud OSS

ES mapping之keyword;term查询添加keyword查询;更改mapping keyword类型

数字臧品系统开发 NFT数字臧品系统异常处理源码分享

日常知识科普

成功解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versi

作为一名开发者,对你影响最深的书籍是哪一本?
随机推荐
Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
阿里OSS对象存储服务
Mots clés pour la cartographie es; Ajouter une requête par mot - clé à la requête term; Changer le type de mot - clé de cartographie
常见的缺陷管理工具——禅道,从安装到使用手把手教会你
Go language concurrency model mpg model
Keras深度学习实战(11)——可视化神经网络中间层输出
Virtual machines on the same distributed port group but different hosts cannot communicate with each other
R language constructs regression model diagnosis (normality is invalid), performs variable transformation, and uses powertransform function in car package to perform box Cox transform to normality on
A simple and powerful developer toolkit box3 cc
In the eyes of the universe, how to correctly care about counting East and West?
高薪程序员&面试题精讲系列115之Redis缓存如何实现?怎么发现热key?缓存时可能存在哪些问题?
GO语言并发模型-MPG模型
Method of inputting dots under letters in markdown/latex
数据库注意事项
ESP32系列--ESP32各个系列对比
测试 H5 和小程序的区别,你真的知道吗?
Alibaba OSS object storage service
CVPR2022 | 可精简域适应
postgresql之词法分析简介
MySQL log management, backup and recovery