search
HomeDatabaseRedisHow to use redis bit operations

The redis test code in this article is based on the following environment:

Operating system: Mac OS 64 bit

Version: Redis 5.0.7 64 bit

Running mode: standalone mode

redis bit operation

reids bit operation is also called bit array operation and bitmap. It provides four commands: SETBIT, GETBIT, BITCOUNT, and BITTOP for operating binary bit arrays.

Let’s take a look at a basic operation example

How to use redis bit operations

SETBIT

Syntax: SETBIT key offset value

That is: Command key offset 0/1

The setbit command is used to write the binary bit setting value of the specified offset in the bit array, The offset starts counting from 0, and only 1 or 0 is allowed to be written. If a value other than 0 and 1 is written, the writing fails:

How to use redis bit operations

GETBIT

Syntax: GETBIT key offset

That is: Command key offset

The gitbit command is used to obtain Binary value at the specified offset in the bit array:

How to use redis bit operations

BITCOUNT

Syntax: BITCOUNT key

That is: Command key

The bitcount command is used to get the number of binary bits with a value of 1 in the bit array of the specified key. Before we wrote the offset 0 has a value of 1, offset 10 has a value of 1, and offset 8 has a value of 0:

How to use redis bit operations

BITOP

Syntax: BITOP operation destkey key [key...]

That is: Command operation result target key key1 key2...

bitop The command can perform and (bitwise AND), or (bitwise OR), xor (bitwise exclusive OR) operations on the keys of multiple bit arrays, and set the operation results to destkey:

How to use redis bit operations

Underlying data structure analysis

SDS is a data structure in redis, called Simple Dynamic String, and it is a binary Safe, in most cases strings in redis are stored using SDS.

Data structure of SDS:

struct sdshdr {   #记录buff数组中已使用字节的数量   #也是SDS所保存字符串的长度   int len;   #记录buff数组中未使用字节的数量   int free;   #字节数组,字符串就存储在这个数组里   char buff[];  }

Data storage example:

How to use redis bit operations

##Picture source "Redis Design and Implementation"

## Advantages of #SDS:

    Hongmeng official strategic cooperation and co-construction - HarmonyOS technology community
  1. Time complexity is O(1)
  2. Prevent buffer overflow
  3. Reduce the number of memory reallocations required when modifying the string length
  4. Binary safe API operation
  5. Compatible with some C string functions
For a detailed introduction to SDS, please refer to "Redis Design and Realization" article.

The bit array in redis is stored in the String string data format, and the string object uses the SDS simple dynamic string data structure mentioned above.

How to use redis bit operationsPicture source "Redis Design and Implementation"

Everyone knows that a byte is stored with 8 binary bits, that is 8 0s or 1s, that is, one byte can store decimal numbers from 0 to 127, which includes all numbers, English upper and lower case letters, and punctuation marks.

1Byte=8bit


1KB=1024Byte


1MB=1024KB


1GB=1024MB

Bit array In the redis storage world, each byte is also 8 bits, and the initial value is:

0 0 0 0 0 0 0 0

The bit operation is to set 0 or 1 on the corresponding offset offset, for example, set the third bit to 1, that is:

0 0 0 0 1 0 0 0  #对应redis操作即:  setbit key 3 1

Based on this, if you want to set the offset to 13 The position is set to 1, that is:

setbit key 13 1  #对应redis中的存储为:  0 0 1 0 | 0 0 0 0 | 0 0 0 0 | 1 0 0 0

Time complexity


##GETBIT command time complexity O(1)


STEBIT command time complexity O(1)


BITCOUNT command time complexity O(n)


The time complexity of the BITOP command is O(n), O(n2)

Let’s look at why the time complexity of the GETBIT and SETBIT commands is O(1). When we When executing a SETBIT key 10086 1 value, reids are calculated as follows:

Get which byte to be written in the bit array: 10086÷8=1260, which needs to be written to the subscript of the bit array The byte of 1260

Gets the number of this byte to be written: 10086 mod 8 = 6. It needs to be written to the index of this byte which is 6, that is, the 7th bit.

Through these two calculation methods, you can clearly see that GETBIT and SETBIT of bit operations are constant calculations, so their time complexity is O(1).

The BITCOUNT command needs to traverse all the elements of the entire bit array to calculate how many elements have a value of 1. Of course, redis will have a set of complex optimization algorithms for executing the bitcount command on bits with big data, but the core The idea is still the same, it is nothing more than reducing the number of partial traversal queries. If 128 bits are explicitly used as one traversal, then the number of times he needs to traverse is equal to all the digits divided by 128.

The BITTOP command has different execution methods according to different operations. For example, for AND operation, you need to check the bit value is 1.

Storage Space Calculation

Based on the above introduction, we can know how to calculate the memory size occupied by using the Redis-based bit array data structure to store data. For example, if there are 10 billion data, then the byte array it requires:

1000000000÷8÷1024÷1024≈119.21MB

That is, only about 119MB of memory is needed to store 1 billion data Space, this is no problem at all for the 16G and 32G cluster versions of redis that are now available.

It should be noted that if the amount of your data is not large, then don’t make the starting offset very large. This will also take up space. For example, we only need to store a few hundred pieces of data, but The offset is very large, which will cause a lot of waste of memory space.

Application scenarios

In actual project development, there are many businesses that are suitable to be implemented using redis bits.

User sign-in scenario

The daily date string is used as a key, the user ID is used as the offset, and the daily user sign-in status is counted, and the total user sign-in number

Statistics on the number of active users

User daily activity, monthly activity, retention rate, etc. can be stored using redis bit arrays, or use the daily date as the key. Write when the user is active Enter offset as the bit value 1 of the user ID.

The same goes for monthly living expenses.

Whether the user is online and the total number of people online

Use the same bit array, set the bit offset of the user ID mapping to 1 to indicate online, and to 0 Indicates offline. This can realize user online and offline queries and statistics of the total number of people online

The global message prompt of users in the APP is a red dot

Now most APPs have in-site With the message function, when there is a message, a small red dot will be prompted, indicating that the user has new messages.

The above is the detailed content of How to use redis bit operations. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

redis error什么意思redis error什么意思Jun 17, 2019 am 11:07 AM

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)