Redis several practical applications

 

1. Distributed Lock:

 

Achieved: value ex time nx implemented instructions set key, the command and its parameters are atomic operations. The lock is released by del key, to compare the same value before the release whether the current value, the reasons given below. key to lock name. value is a random number and the random number with a record variable. ex parameter is a set of instructions, on behalf of the key automatically deleted second time. nx is a parameter of the instruction set, which means no key when setting its value.

 

Description: Sets the expiration time is occupied locks to prevent abnormal thread appears, has not led to the release of the lock. The reason value using random numbers: After the timeout, the new thread gets locked and set a new random number as a key, the original thread has been implementing logic, the final and executed del instructions, if the random number in deleted compared before, and do not put key to delete. Prevent the other thread releases the lock for the current thread.

 

2. Message Queue:

 

 

Note: Use blocking read instead of polling is to prevent the queue is empty, do a lot of useless empty polling.

 

3. Use the bit map memory bool type data:

 

Description: Bitmap Detailed instructions may own search. First explain the bitmap bit is bit of Chinese name, the smallest unit of storage is a bit bitmap, part of an array structure, by setting the index data. A binary bit which is 0 or 1, a byte (byte) equal to the eight bit. A letter is equal to one byte. Characters not necessarily, different coding occupied bytes, two bytes some some three bytes. Setbit can set the position, such as setbit key 1 1, meaning if set to 0 in the second position not set, the default is 0.

 

Achieved: Redis bitcount and provides instructions to count and locate bitpos bitmap data type, can be added later selection range start and end parameters. bool just as long as the type of data 0 and 1, when there is a large amount of data such bool, using bitmap can save a very large space. Scenarios should be similar to the login records, attendance records this pure bool type data. For example, a person A sign of the third day, the implementation setbit Akey 2 1. And so on, I would like to perform bitcount Akey when attendance statistics for several days. The return is the number 1, that is, the number of attendance days. bitpos position 1 is acquired first appears as bitpos Akey 1. returns the position of the first occurrence of 1, i.e. A check is when the first day.

 

4.HyperLogLog statistics UV:

 

Description: HyperLogLog Redis is an advanced data structure, the role of the statistical base, such elements have 1,2,3,4,5,5,6. HyperLogLog only statistical base 6, ie, the number of non-repetition of 1,2,3,4,5,6 elements, the most important thing is, even if the elements are very large, memory is fixed in its possession, each HyperLogLog only it takes 12KB. It's little instruction. There are: pfadd key element to add elements. pfcount key statistical base.

 

Implementation: When do UV, namely statistics on the number of unique visitors that he can spend for the whole site, a single page can be. We just need to ip (or user id) and date (accurate to the day) added to it on the line, there are many more information is also OK, but HyperLogLog only statistical base. Within a day as long as the user's information is absolutely the same as on the line, it will automatically go heavy, which is a day no matter how many times users access to only count once.

 

The search for a specific scan key:

 

Description: When the data amount is not large, the keys can be directly used simple instructions, according to certain rules corresponding to the search key. But after the large amount of data is clearly not desirable, the most important is that keys are blocked, it will go through all key until you find qualified key, the time complexity is O (n). Although the scan time complexity is O (n), but it does not block thread may define the number of return. scan is a series of instructions, instructions for some of the different types of basic data has a corresponding search key is just the most basic usage, usage can find more detailed official api documentation.

 

Implementation: using the command scan start match key * count limit. start cursor is the place to start, precisely because it is used by the cursor, so it will not cause thread blocks. Back then match the regular expression. followed by the return of the count defining the number. For example: scan 0 match person * count 1000, the cursor from 0, the return key person up to the beginning of 1000. Returns two results, one is to stop the cursor where is an integer, 0 put into this next integer then find. Another is to find the key.

 

 

 

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162649.htm