preface

What are some of the most common Redis data structures?

Probably most of the answers are string, hash, list, set, zset. Of course, there’s no mistake about this answer, but believe it and your interviewer will probably hear it.

Itself we choose the industry competition is extremely strong, but don’t even have to spell knowledge to spell it??

The most commonly used data structures are string, hash, list, set, and zset, but I might also use Hyperloglog and Bitmap. I’m sure the interviewer will be impressed when he hears your answer!

Without further ado, let’s begin, ⬇



Hyperloglog



Hyperloglog profile

Hyperloglog is a probabilistic data structure used to estimate the cardinality of data.

<font size=”3″>** base: <font size=”3″>** base


For a set 1, 2, 3, 4, then its cardinality is 4


For a set 1, 2, 3, 4, 1, the cardinality is also 4**



Hyperloglog role

We can use it to count the UVs.

<font size=”3″>
UV is: UniqueVisitor, UV refers to == the number of unique visitors ==, a computer is considered a UniqueVisitor. A computer that visits the same site once in the morning and again in the afternoon can only be counted once.


It can be understood as a Set, so why do I use it to count UV?

**Redis HyperlogLog reduces memory consumption by sacrificing accuracy. It requires only 12K of memory and is able to count 2^64 data with a standard error of 0.81%. On the other hand, SET needs to consume a lot of space, so whether Hyperloglog is suitable for scenarios where accuracy is not high, such as statistical interval activity. 台湾国

Why it can be stored like this, mainly depends on the Bernoulli experiment, you can go to Baidu to understand.



Use on the command line

  • Pfadd \<key> [element] : Add data
  • Pfcount \<key> : Count






Use in SpringBoot

@Test
public void testHyperloglog() {

    String key = "language";

    for (int i = 1; i <= 10000; i++) {
        redisTemplate.opsForHyperLogLog().add(key,i);
    }

    for (int i = 5000; i <= 15000; i++) {
        redisTemplate.opsForHyperLogLog().add(key,i);
    }

    for (int i = 10000; i <= 20000; i++) {
        redisTemplate.opsForHyperLogLog().add(key,i);
    }

    long size = redisTemplate.opsForHyperLogLog().size(key);
    System.out.println(size);
}



It can be seen that the result value is 19891, which is not much different from the real value: 20000. Although there is an error, it is very good compared with SET!

In addition, SpringBoot can also merge multiple keys and count the amount of data after the merge

@Test
public void testHyperloglog() {

    String key1 = "language1";
    String key2 = "language2";
    String key3 = "language3";
    String unionKey = "language";


    for (int i = 1; i <= 10000; i++) {
        redisTemplate.opsForHyperLogLog().add(key1,i);
    }

    for (int i = 5000; i <= 15000; i++) {
        redisTemplate.opsForHyperLogLog().add(key2,i);
    }

    for (int i = 10000; i <= 20000; i++) {
        redisTemplate.opsForHyperLogLog().add(key3,i);
    }

    redisTemplate.opsForHyperLogLog().union(unionKey,key1,key2,key3);

    long size = redisTemplate.opsForHyperLogLog().size(unionKey);
    System.out.println(size);
}



So the number is still 19,991



Bitmap



Bitmap profile

A bitmap is not a special data structure. It is simply a string called a byte array (for those who know about Bloon filters).

The corresponding value or state of an element is represented by a bit, where the key is the corresponding element itself.

Bit operations are divided into two groups:

  • Single-bit operations at fixed times (such as setting a bit to 1 or 0 or getting its value)
  • Operations on a set of bits, such as counting the number of bits set to a location range (for example, a population count).

One of the biggest advantages of bitmaps is that they often save a lot of space when storing information. For example, on a system that represents different users as incremental user IDs, only 512 MB of memory can be used to remember one bit of information for 4 billion users



Bitmap effect

Usage scenarios

  • All kinds of real-time analysis.
  • Stores spatially efficient but high-performance Boolean information associated with an object ID.

We can use it to count DAU.

<font size=”3″>
== Number of Daily Active Users ==(Daily Active User (DAU)) is a statistic used to reflect the operation of a website, Internet application, or online game. Daily active users are usually counted as the number of users (excluding repeat users) who have logged in or used a particular product on a single day (count day).



Command line with Bitmap

Set and retrieve bits using setbit and getbit commands:

  • The setbit command takes the bit number as its first argument and sets it to a value of 1 or 0 as its second argument. This command automatically enlarges the string if the bits addressed exceed the current string length.
  • GetBit simply returns the value of the bit at the specified index. Out-of-range bits (bits that address beyond the length of the string stored in the target key) are always treated as zero.

There are also three commands on the bitgroup:

  • Bitop performs bitwise operations between different strings. The operations provided are AND, OR, XOR, AND NOT.
  • BitCount performs the fill count, reporting the number of bits set to 1.
  • Bitpos finds the first place with the specified value 0 or 1.



SpringBoot use Bitmap

@Test
public void testBitmap() {

    String key = "bitmap";

    redisTemplate.opsForValue().setBit(key,1,true);
    redisTemplate.opsForValue().setBit(key,4,true);
    redisTemplate.opsForValue().setBit(key,2,true);
    redisTemplate.opsForValue().setBit(key,5,true);

    System.out.println(redisTemplate.opsForValue().getBit(key,2));
    System.out.println(redisTemplate.opsForValue().getBit(key,3));
    System.out.println(redisTemplate.opsForValue().getBit(key,5));

}


Stern said

I am aCode pipi shrimp, a love of sharing knowledge of mantis shrimp lovers, in the future will continue to update the beneficial blog, look forward to your attention!!

It is not easy to create, if this blog post is helpful to you, I hope you can == thumb up and pay attention to me, thank you for your support, we will see you next time ~~~

== Share outline ==

Big factory interview topic column






Java from entry to the grave learning route directory index






Open source crawler example tutorial directory index


Pay attention to the public number, more exciting content in which!!