Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of GETBIT and SETBIT in Redis

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "detailed introduction of GETBIT and SETBIT in Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

GETBIT and SETBIT in Redis

Redis is the database of in-memery, and its advantages are self-evident.

You can read the introduction on the official website for details. Https://redis.io

There are five main data types: strings,lists,sets,hashes.

When I learned about common commands of type strings, I didn't know much about the meaning of GETBIT and SETBIT, so I searched for related articles. I feel very powerful when I read an article introducing the application of GETBIT and SETBIT. The record is as follows:

When we log on to some blog sites or video sites, the site often records whether we have read an article or watched a video.

If it is implemented with the traditional mysql database, if there are a large number of users and many articles and videos, it will bring a lot of pressure to the database.

GETBIT and SETBIT with Redis are much easier.

Let's take video as an example. We use bitmap to record whether users have watched a video, and each video corresponds to a bitmap. For example

Key: video:1201value: 000000...0000

Key is marked with the video name video+ colon + id.

Value is a bitmap. One (bit) has two possibilities, 0 or 1. 0 means you haven't seen it, 1 means you've already seen it.

The offset represents the user id. For example, the 200th bit represents whether a user with a user_id of 200 has watched a video with an id of 1201.

The command above is to set the ID to 200. users who have watched the video with an ID of 1201 have already seen it. The command above query # GETBIT key offsetGETBIT video:1201 20videos is to query whether a user with an ID of 200has watched a video with an ID of 1201.

Of course, you can also correspond to a bit in a bitmap,bitmap to indicate whether a video has been watched.

In addition, the very popular sign-in or login records can also be implemented with a similar design.

For example, use a bitmap to record the login of all users. One of the bitmap represents whether a user has logged in that day. 0 indicates that he has not logged in, and 1 represents that he has logged in.

Generate a bitmap every day.

You can count active users and other operations by counting bitmap for many days.

This is the end of the detailed introduction of GETBIT and SETBIT in Redis. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report