当前位置:网站首页>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 .
边栏推荐
- Some basic database operations (providing the original database information)
- [learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
- ESP32系列--ESP32各个系列对比
- 如何避免下重复订单
- Virtual machines on the same distributed port group but different hosts cannot communicate with each other
- postgresql 之 ilist
- Common sense knowledge points
- 数字臧品系统开发 NFT数字臧品系统异常处理源码分享
- 【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维
- Win10 system problems
猜你喜欢

postgresql之词法分析简介

Overview of SAP marketing cloud functions (IV)

Linux 安装 CenOS7 MySQL - 8.0.26

Multimeter resistance measurement diagram and precautions

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

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

入行 4 年,跳槽 2 次,我摸透了软件测试这一行

The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment

ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)

laravel8使用faker调用工厂填充数据
随机推荐
Cvpr2022 𞓜 thin domain adaptation
股票开户要找谁?在线开户安全么?
[untitled]
I have been in the industry for 4 years and have changed jobs twice. I have learned a lot about software testing
Linux 安装 CenOS7 MySQL - 8.0.26
成功解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versi
【ansible问题处理】远程执行用户环境变量加载问题
在同花顺开户证券安全吗,需要什么准备
简谈企业Power BI CI /CD 实施框架
Laravel 8 realizes auth login
作为一名开发者,对你影响最深的书籍是哪一本?
laravel 8 实现Auth登录
A simple and powerful developer toolkit box3 cc
不要小看了积分商城,它的作用可以很大
MES在流程和离散制造企业的15个差别(下)
IDEA连接mysql自定义生成实体类代码
Bert whitening vector dimension reduction and its application
List of PostgreSQL
Unit contour creation method
R语言构建回归模型诊断(正态性无效)、进行变量变换、使用car包中的powerTransform函数对目标变量进行Box-Cox变换(Box–Cox transform to normality)