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

How to use Bitmap of Redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this article, the editor introduces in detail "how to use Redis's Bitmap". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use Redis's Bitmap" can help you solve your doubts.

i. Basically use 1. Configuration

We use SpringBoot 2.2.1.RELEASE to build the project environment and add redis dependencies directly to pom.xml

Org.springframework.boot spring-boot-starter-data-redis

If our redis is the default configuration, no additional configuration can be added, or directly in the application.yml configuration, as follows

Spring: redis: host: 127.0.0.1 port: 6379 password:2. Use posture

Bitmap has three main operation commands, setbit,getbit and bitcount

a. Set markup

Setbit, which mainly refers to setting an index to 1 (setting 0 to erase the tag). The basic syntax is as follows

# Please note that the index must be a number, and the value after it must be 0/1setbit key index 0tabl

In the corresponding SpringBoot, it can be easily implemented with the help of RestTemplate, and there are usually two ways to write it, either of which can be written.

@ Autowiredprivate StringRedisTemplate redisTemplate;/** * set the tag bit * * @ param key * @ param offset * @ param tag * @ return * / public Boolean mark (String key, long offset, boolean tag) {return redisTemplate.opsForValue () .setBit (key, offset, tag) } public Boolean mark2 (String key, long offset, boolean tag) {return redisTemplate.execute (new RedisCallback () {@ Override public Boolean doInRedis (RedisConnection connection) throws DataAccessException {return connection.setBit (key.getBytes (), offset, tag);}});}

The core difference between the above two writing methods is the serialization of key. The first method uses the default jdk string serialization, which is somewhat different from the following getBytes (). About this, interested partners can take a look at my previous blog post: RedisTemplate configuration and using # serialization

b. Judge whether it exists or not

That is, getbit key index. If 1 is returned, it means it exists, otherwise it does not exist.

/ * determine whether it has been marked * * @ param key * @ param offest * @ return * / public Boolean container (String key, long offest) {return redisTemplate.opsForValue () .getBit (key, offest);} c. Count

Namely bitcount key, statistics and

/ * * param key * @ return * / public long bitCount (String key) {return redisTemplate.execute (new RedisCallback () {@ Override public Long doInRedis (RedisConnection redisConnection) throws DataAccessException {return redisConnection.bitCount (key.getBytes ());}});} 3. Application scenario

The previous basic use is relatively simple. As mentioned in the introduction of the String data structure, we need to focus on the usage scenario of bitmap, what it can be used for, and the scenarios in which it will have significant advantages.

Daily active users statistics

Give the thumbs-up

Bloomfilter

Although the above three scenarios have similarities, there are still some differences in the actual application scenarios. Let's explain them one by one.

a. Daily active users statistics

Count the number of daily active users of an application or website, which belongs to the more common case. If you use redis to do this, the first thing we can easily think of is the Hash structure. The general logic is as follows.

According to the date, set key. If today is 2020-10-13, then key can be app_20_10_13.

Secondly, when the user accesses it, set field to userId and value to true

To judge the number of daily active users is to count the number of map hlen app_20_10_13

Is there something wrong with the above logic? Of course, no problem, but think about it, when our app is very nb, the number of daily active users is millions, and when there are tens of millions of users, this memory overhead is a bit scary.

Let's take a look at what bitmap can do.

Also set key based on date

When the user accesses, index is set to userId,setbit app_20_10_13 uesrId 1

Daily active users Statistics bitcount app_20_10_13

A brief comparison of the above two schemes

When the amount of data is small and the distribution of userid is uneven, the small ones are single digits, the large ones are tens of millions, hundreds of millions of this kind, it is a bit bad to use bitmap, because userId is used as index, then the length of bitmap needs to be able to accommodate the largest userId, but the actual number of daily active users is very small, indicating that there are a lot of blank data in the middle of bitmap.

On the other hand, when the amount of data is very large, such as million / 10 million, and userId is continuously increasing, bitmap has two advantages: 1. The storage overhead is small, 2. The total statistics are fast.

c. Give the thumbs-up

The most important thing about the like business is that after a user likes, he cannot continue to like (except for some business scenarios, of course), so we need to know whether we can continue to like.

Of course, the above hash can also be implemented, but here we mainly discuss the implementation logic of bitmap.

For example, if we want to like an article, then we generate redisKey=like_1121 according to the article articleId and use userId as index.

First of all, getbit like_1121 userId is used to determine whether likes have been given, so as to limit whether users can operate.

The choices of Hash and bitmap are similar to those considered above.

d. Bloom filter bloomfilter

The Bloom filter is famous. Here's a brief introduction to what this thing is.

The underlying storage is a bitmap

When a data is given, n values are obtained through n hash functions.

Map the n values obtained by hash to bitmap, and the corresponding position of the tag is 1.

If a piece of data is calculated by hash, if the n values and the corresponding bitmap are all 1, then the data may exist; if there is a non-1, it means that the data must not exist

Please note: if it does not exist, it must not exist; when it does, it may not exist.

As we know from the above description, the underlying data structure of bloomfilter is bitmap, and of course its key point is the hash algorithm. According to the characteristic that it must not exist when it is missed, it is very suitable for solving the problem of cache breakdown.

Experience description

Redis's Bloom filter is mainly targeted at > = 4.0.It is provided in the form of plug-ins. The source address of the project is https://github.com/RedisBloom/RedisBloom. According to the instructions of readme, let's simply experience the posture of using bloomfilter in redis.

# install docker run-p 6379docker exec 6379 in docker mode-- name redis-redisbloom redislabs/rebloom:latest# accesses docker exec-it redis-redisbloom bash# via redis-cli. Start using # redis-cli127.0.0.1:6379 > keys * (empty array) 127.0.0.1 name redis-redisbloom redislabs/rebloom:latest# 6379 > bf.add newFilter hello (integer) 1127.0.0.1 integer 6379 > bf.exists newFilter hello (integer) 1127.0.0.1docker exec 6379 > bf.exists newFilter hell (integer) 0

The use of bloomfilter is relatively simple, mainly two commands bf.add to add elements, bf.exists to determine whether it exists, please note that it is not deleted.

After reading this, the article "how to use the Bitmap of Redis" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report