Are you sure you really know redis The second series

Interview on the little X to make it through, and today conducted a second round of interviews, so they have the following dialogue. Interviewer: I now have a scene needs, such as our APP wants to be a user to check in, I want to count the number of times a user check-ins this year, but I have a special request, redis you want to use to do this, what do you think it? Interviewer: This simple ah, I can use a simple string to achieve, but in the design of key time, with the format usercode + date, value casual will do, if you were thumbs action, then go out into the redis cache a record on the line, such as set zhangsan20191225 1, and then when I monthly or annual statistical user check-ins when it is in accordance with the rules of assembled key, then turn to redis acquire a corresponding value, then value do some statistics on it . Interviewer: ah, you this really is a good idea, but you still have a problem that I sign a user such as a year, then the same user you need to give him build 365 key-value, the memory footprint is too serious, If the users of our systems hundred million, and the machine's memory can be stored under it? There is no other method can save memory of it? Interviewer: the amount? Well. . . The next step is today's hero leads a, redis bitmap, bitmap believe many people have not heard, this is a new data structure redis support? Definitely not. In fact, the bitmap is our last string of articles introduced through the introduction section, I believe we all know, the string is actually stored in the underlying data structure is an array of bytes, since it is a byte array, then we can not operate by word mode bit (bit) of the section to operate it? The answer of course is yes, redis provides such a command for us, is the corresponding bitmap. Back to top face questions, in fact, we can use a bitmap for processing, in fact, every time a user sign occupies only one bit (bit), so even if users sign 365 days also just take 365 bit (bit), also doing to 46 characters, a normal string can hold down completely, so you can save a lot of memory. We mentioned above, the bitmap is not a special data structure, that is, ordinary string redis, so we can use ordinary get / set commands direct access or content of the bitmap, can also use the command bitmap operations getbit / setbit command byte as an array of bits set to operate, redis bit array expansion is automatic, so even if we set an offset position beyond the scope of existing content, redis will automatically be set to zero digit expansion. Next we will be set by the bitmap action string "hello", instead of using the set command, first of all we need to know the number of binary ascii code for each character of "hello" string inside. h: 01101000 High -> Low e: 01100101 l: 01101100 o: 01101111 Before setting, first we must be clear that the high binary number of our common character encoding corresponding figure in the left, lower right, and the median group is from the most start on the left, so low in the left, upper right. FIG e.g. the low bit position of the top row of the binary number of characters us, and the lower line is the low order bit group of bits. Therefore, when the stored bits are set from left to right, then the binary value or the corresponding encoded characters stored in the order from right to left in each byte or character. ! [File] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105626688-1420297077.jpg) Next, we will first bitmap command of the 'h' and 'e' both character set in place the array, which is the first 8-bit and 8-bit to 16-bit bit array, but set the time, we just need to set the corresponding position to position 1 on the list for the position 0, redis automatically help us fill 0, 2, 4 h need to set the location, and e need to set 9,10,13,15 position, and so the other set on the line. ! [File] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105626940-1382396813. jpg) above us in this way is a "bit value stored by the string value" approach, in fact there are "in accordance with the value stored in bit-string value" or "bit-by-bit value stored-value", etc., bit value is stored using the redis setbit command bits sequentially set, and the value is stored as strings using a set command directly to fill the entire digit string and then the entire group of all overwrite the old value. Bit by bit values ​​stored-value:! [File] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105627165-928119545.jpg) according to the string value provided by bits Value:! [ file] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105627404-2028624638.jpg) wherein if the corresponding byte characters can not be printed, then redis will return to its hexadecimal value! [file] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105627864-356351549.jpg) in fact, by the above explanation, we have completed the statistics we want, but there is a defect is that we have to put all the values ​​get into the program, and then use the program to count, if redis can give me some statistics command, we only need to use these commands you can get the result we want and that the more cool , then congratulations, you can steal a lazy, redis provides two instructions to us for our use. bitcount: Bitmap statistics instruction, the number 1 within a range of specified appears. bitpos: used to find the first occurrence 0 or 1 in the specified range. So back to the beginning of the interview questions, we can count the number of times a user sign that year by bitcount, using bitpos command to locate the user first sign of the times. And when we use the above two instructions may also specify a range [start, end], used to count the number of times within a certain range of user check-ins, or from the first day for attendance. But there is a defect, start and end byte index parameter, so we must specify the range of a multiple of 8, and can not be arbitrarily specified. So we wanted to count a month, this might not be true, since that month's data may be scattered in three bytes, we must take all three bytes of data out, and then take the substring in memory the statistics. ! [File] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105628120-1581342964.jpg) last above our setbit and getbit are operating only a single value, if we want more than one operation values, we can use the pipe, but fortunately redis3 provides us after a batch operation bitfield command, this command has three sub-commands are set / get / incrby, can read and write to the specified bit segment, However, a continuous process can only 64 bits, if more than 64 require the use of a plurality of sub-instructions, and may be disposable bitfield plurality of different sub-commands. Using a plurality of sub-time instruction! [File] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105628344-1178159605.jpg) Next, we will use the set subcommand to replace the first letter h a, where a 97 bit ascii code, the command will return the old value. ! [File] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105628523-189536575. jpg) Finally, we look incrby sub-command, incrby sub-command to place within the specified range increment operator, but because it is the increment operator, there might exceed the range that can be represented in binary, such as increasing integer, then overflow upward, increasing the negative, there will be downward overflow, redis default policy is taken exhumation, if an overflow occurs, the sign bit will be discarded directly, such as unsigned after increasing from 1 255 to overflow, and on after after after all become 0, discarding the most significant bit of the sign bit is 0, then such 8-bit unsigned integer 127 is incremented by one, they overflow, we discard the highest sign bit is -128. redis addition also provides a folded wrap sat saturated truncation error is not performed and fail, we may be manually selected by the sub-instruction overflow us some way. Examples of the wrap is folded default:! [File] (https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105630218-988166347.jpg) to manually set the sat saturated truncated example:! [File] ( https://img2018.cnblogs.com/blog/1917499/202001/1917499-20200109105632296-1630549088.jpg) last look at the example given is not to fail to perform manual setup:! [file] (https: //img2018.cnblogs .com / blog / 1917499/202001 / 1917499-20200109105634702-25059067.jpg)

Guess you like

Origin www.cnblogs.com/sheldon1988/p/12170259.html