Interview frequently asked Redis seven kinds of data types, you really understand it?

Foreword

Redis is not a simple key-value pair storage, it is actually a support structure to store various types of data. In a conventional storage key, the key is associated with the string to the string value, but in Redis, these values ​​are not limited to simple string, may also support more complex data structures. Here is supported by Redis data structures:

  • String (String): Binary safe string.
  • List (List): The sorted list of strings element insertion order, based on a linked list implementation.
  • Set (Set): the set of unique scrambled string elements.
  • Ordered set (Sorted Set): with a collection of similar, but each element in the string of numbers associated with a score called. Element is always sorted according to their score, and certain elements may be retrieved score range.
  • Hash (Hash): mapping, by a field and a value field associated with the component values are strings.
  • Bitmap (Bitmap): the same operation as the operation of the bit array string value, a bit can be set and cleared on all counts the bit 1, bit 1 is the first to find to find first set bit 0 and many more.
  • HyperLogLogs : a probabilistic data structure using a small memory space to count the number of unique elements, the error is less than 1%.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

键 (Key)

Binary safe, which means you can use any binary sequence as a key, can be OneMoreStudysuch a string, you can also make the contents of an image file, an empty string is valid . However, there are other rules:

  • Do not use too long , such as a 1KB key. Not only is much of the problem in terms of memory, but focused look at data may require some time-consuming compared. If there is a relatively large , it first hash (such as: MD5, ) SHA1it is a good idea.
  • Do not use too short , such as: OMS100frespect one-more-study:100:fans, the latter is more readable. May consume more memory, but relative to the share of the value of memory, the additional memory is still a lot smaller. We need to find a balance point, not long nor short.
  • A plurality of fields separated by a colon, a plurality of words in the fields or separated by hyphens, for example: one-more-study:100:fansor one.more.study:100:fans.
  • The maximum allowed is 512MB.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

String (String)

String type and is associated with the most simple type. It is the only Memcached data type, so for the novice, to use it in Redis is also very easy. Type is a string, when we use the value as a character string type, we can map from one string to another. String data type has many application scenarios, such as caching or fragment HTML pages.

The following outlines the command string (in redis-cli in):

> set one-more-key OneMoreStudy
OK
> get one-more-key
"OneMoreStudy"

Use SETand the GETway the command to set and query string value. Note that, if the current has a string value and associated SETcommand will replace the stored prior values. String can be arbitrary binary data, such as jpeg images. The string can not exceed a maximum of 512MB. SETThere are some practical command of optional parameters, such as:

> set one-more-key Java nx   #如果key存在,则设置失败。
(nil)
> set one-more-key Java xx   #如果key存在,才设置成功。
OK

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Although the basic value Redis string, but they may also be used to perform some useful operation. such as:

> set one-more-counter 50
OK
> incr one-more-counter   #自增加1
(integer) 51
> incr one-more-counter   #自增加1
(integer) 52
> incrby one-more-counter 5   #自增加5
(integer) 57

INCRCommand string value resolution is an integer, which increases from 1, the final value obtained as a new value. There are other similar commands, for example INCRBY, DECRand DECRBYcommands. INCRCommand is atomic, instant multiple clients simultaneously is the same key INCRcommand, it does not enter the race conditions. For example, the above example the first set one-more-counteris 50, even though both clients to issue command INCR, then the final value 52 is certainly.

It can be used MSETand MGETcommand sets or more queries in a single command value, reducing the delay is also useful. such as:

> mset a 1 b 2 c 3
OK
> mget a b c
1) "1"
2) "2"
3) "3"

Use MGETcommand, Redis returns an array of values.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Use the DEL command can delete and values associated with the presence of the specified returns 1 if there is no specified 0 is returned. Use EXISTScommand determination that the specified Redis the presence of the specified return a specified does not exist is returned to zero. such as:

> set one-more-key OneMoreStudy
OK
> exists one-more-key
(integer) 1
> del one-more-key
(integer) 1
> exists one-more-key
(integer) 0

Use TYPEcommand to return data type stored in the specified key value, such as:

> set one-more-key OneMoreStudy
OK
> type one-more-key
string
> del one-more-key
(integer) 1
> type one-more-key
none

Before discussing more complex data structures, we need to discuss another feature that no matter what type of value is applicable, it is the EXPIREcommand. It can set an expiration time, the expiration time when more than this, that will self-destruct, like this calls the DELcommand. such as:

> set one-more-key OneMoreStudy
OK
> expire one-more-key 5
(integer) 1
> get one-more-key #立刻调用
"OneMoreStudy"
> get one-more-key #5秒钟后调用
(nil)

The above example for the EXPIREcommand to set the expiration time, you can also use the PERSISTcommand to remove the expired time, this will endure. In addition to EXPIREcommands, you can use the SET command to set the expiration time, such as:

> set one-more-key OneMoreStudy ex 10 #设置过期时间为10秒
OK
> ttl one-more-key
(integer) 9

The above example is provided with a string value OneMoreStudyof one-more-keythe expiration time is 10 seconds. After the call TTLcommand to check the remaining time to live.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Expiration time can be set precision seconds or milliseconds, but the resolution is always expiration time of 1 millisecond. In fact, stored on the server is not Redis expiration time length, but the expiration time.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

List (List)

Redis list is implemented using a linked list, which means adding or deleting elements in a head or tail of the time complexity is O (1), very fast. However, according to the corresponding element of the index query time complexity is O (n), much slower. If you want to quickly query large amounts of data, you can use an ordered set, it will be introduced later.

LPUSHCommand to add a new element to the left of the list (at the top), and the RPUSHcommand to a new list element is added to the right (bottom). Finally, the LRANGEcommand can be extracted by a range of elements from the list. such as:

> rpush one-more-list A
(integer) 1
> rpush one-more-list B
(integer) 2
> lpush one-more-list first
(integer) 3
> lrange one-more-list 0 -1
1) "first"
2) "A"
3) "B"

LRANGECommand requires two additional parameters, indexes and index the last element of the first element to be returned. If the index is negative, Redis count starting from the end, -1 is the last element of the list, -2 is the penultimate element of the list, and so on.

LPUSHAnd RPUSHcommand supports multiple parameters, you can use a command to add more elements, such as:

> rpush one-more-list 1 2 3 4 5 "last"
(integer) 9
> lrange one-more-list 0 -1
1) "first"
2) "A"
3) "B"
4) "1"
5) "2"
6) "3"
7) "4"
8) "5"
9) "last"

Redis on the list, you can also remove and return to the elements. And LPUSHand RPUSHcommands is the corresponding LPOPand RPOPcommand, LPOPthe command element is the list on the left (top) is removed and returned, RPOPthe command is an element of the right list (bottom) removal and return. such as:

> rpush one-more-list a b c
(integer) 3
> rpop one-more-list
"c"
> rpop one-more-list
"b"
> rpop one-more-list
"a"

We've added three elements, and removed and returned three elements, then the list is empty, no elements. If you use the RPOPcommand returns a NULLvalue:

> rpop one-more-list
(nil)

Use RPUSHand RPOPcommand, or LPUSHand LPOPcommand stack function may be implemented using LPUSHand the RPOPcommand, or RPUSH, and LPOPthis command queue may be implemented. It can also be achieved producers and consumers modes, such as multiple producers use the LPUSHcommand to add the task to the list, more consumers use the RPOPcommand to remove the task from the list. Sometimes, however, the list may be empty, there is no task to be processed, so the RPOPcommand returns only NULL. In this case, consumers are forced to wait for some time, and then use the RPOPcommand and try again. This has exposed several shortcomings:

  1. Can process between the client and server useless command because all requests in the list is empty at the time of any actual work will not be completed, they will return NULL.
  2. Because consumers receive NULLthen we will wait for a period of time, thus increasing the delay tasking. In order to reduce the delay, we can two calls RPOPwaiting less time between, which expanded more useless calls to the Redis.

Is there any way to solve it? Use BRPOPand BLPOPcommands, they RPOPand LPOPcommand similar, the only difference is: If the list is empty, the command will be blocked until there is a new element added to the list, or when a specified timeout period is up, they will be returned to the calling square. such as:

> brpop tasks 5

It implication is that the list is empty, waiting for the elements in the list, but after 5 seconds if no new element has been added, is returned. You can pass the timeout 0 means wait forever elements are added. Can also pass multiple lists, then check each list in turn by the order parameter and returns the first element of a non-empty list the tail. In addition there are the following three points should be noted:

  1. When the list is empty, and there are multiple clients while waiting, there is a new element is added to the list, it will be the first to get the client to wait, and so on.
  2. The return value RPOPas compared to the command is different, which is an array containing two elements, comprising a key and corresponding elements, as BRPOPand BLPOPcommands from a plurality of elements can be prevented from waiting lists.
  3. Than the timeout time, it will return NULL.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Creation and deletion of the list is done automatically by the Redis, try to not exist when you add elements, Redis will automatically create an empty list; when the last element is removed, Redis will automatically delete the list. This is not specific to the list, it applies to all types Redis data from a plurality of elements, such as collections, ordered set, hash, they have three rules:

  1. When we add an element to aggregate data types, if the target does not exist, before adding elements create an empty aggregate data types. such as:
> del one-more-list
(integer) 1
> lpush one-more-list 1 2 3
(integer) 3

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

However, at present, the operation can not be wrong data type, such as:

> set one-more-key OneMoreStudy
OK
> lpush one-more-key 1 2 3
(error) WRONGTYPE Operation against a key holding the wrong kind of value
> type one-more-key
string
  1. When we remove the element from the aggregate data types, if this value remains empty, the key will be automatically destroyed. such as:
> lpush one-more-list 1 2 3
(integer) 3
> exists one-more-list
(integer) 1
> lpop one-more-list
"3"
> lpop one-more-list
"2"
> lpop one-more-list
"1"
> exists one-more-list
(integer) 0
  1. When the corresponding key is not present, and calls a read-only commands (such as LLENcommands for a list length) or write command (e.g., LPOPcommand), returns null data type polymerization results. such as:
> del one-more-list
(integer) 0
> llen one-more-list
(integer) 0
> lpop one-more-list
(nil)

Redis pursuit of high performance, internal list of implementation is not a simple linked list, where the first secrecy, the follow-up article will detail.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Collection (Set)

Collection is a set of unordered strings, SADDcommand to add new elements to the collection. May also be set to a number of other operations, such as: determining whether the given element exists, the intersection between a plurality of sets performed, and sets the difference or the like. such as:

> sadd one-more-set 1 2 3
(integer) 3
> smembers one-more-set
1) "1"
2) "3"
3) "2"

In the above example, the collection adds three elements, and let Redis return all elements. As you can see, the return element is not sorted. In each call, the order of elements are likely to be different.

You can also use SISMEMBERcommand determines whether there has been given element, such as:

> sismember one-more-set 3
(integer) 1
> sismember one-more-set 30
(integer) 0

In the above example, the set 3, 1 is returned; and 30 is not set, 0 is returned.

You can use SINTERthe command, a plurality of sets of the calculated intersection; use SUNIONcommand, and calculating a set of a plurality of sets; use SPOPcommand, removes and returns a random element in the set; using the SCARDcommand, the number of elements in the set is calculated. such as:

> sadd one-more-set1 1 2 3
(integer) 3
> sadd one-more-set2 2 3 4
(integer) 3
> sinter one-more-set1 one-more-set2 #交集
1) "3"
2) "2"
> sunion one-more-set1 one-more-set2 #并集
1) "1"
2) "3"
3) "2"
4) "4"
> spop one-more-set1 #随机移除一个元素
"3"
> scard one-more-set1 #元素数量
(integer) 2

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Ordered set (Sorted Set)

Similar to the ordered set is a collection of data and the type of mixing between the hash. Like the collection as an ordered collection of unique, non-repetitive string elements, so in a sense, it is a collection of ordered set. But the elements of the collection is not sorted, and ordered set of each element with a called 分数(score) floating-point value associated with, which is why an ordered set of hash also similar reason, because each elements are mapped to a value. Sort ordered set of rules are as follows:

  • If A and B are two different fractions of elements, then if the score A.> B. Fraction, the A> B.
  • If the score is the same as A and B, then if B is greater than A string sort strings in the dictionary, then the A> B. A and B are not equal to the string, because of the ordered set of elements are unique.

We give an example, the king of glory to add points to the team's name and ordered set, where the clan name as a value, as the team score points.

> zadd kpl 12 "eStarPro"
(integer) 1
> zadd kpl 12 "QGhappy"
(integer) 1
> zadd kpl 10 "XQ"
(integer) 1
> zadd kpl 8 "EDG.M"
(integer) 1
> zadd kpl 8 "RNG.M"
(integer) 1
> zadd kpl 4 "TES"
(integer) 1
> zadd kpl 2 "VG"
(integer) 1

As described above, ZADDthe command and SADDthe command is similar to, but more than one additional parameter (in front of the element to be added) as the score. ZADDCommand also supports multiple parameters, although it is not used in the example above, but you can also specify multiple fractions and value pairs. The use of an ordered set, and quickly return to the list of teams ranked according to their points, because in fact they have been sorted.

Note that, in order to quickly obtain an ordered set of elements, each element of added time complexity are O (log (N)), because the ordered collection is to use both jumping and dictionaries to achieve, specific principle here first secrecy, the follow-up article will detail.

You can use ZRANGEthe command to obtain a corresponding value in ascending order, for example:

> zrange kpl 0 -1
1) "VG"
2) "TES"
3) "EDG.M"
4) "RNG.M"
5) "XQ"
6) "QGhappy"
7) "eStarPro"

0 and -1 represents the query element from the first to the last one. You can also use the ZREVRANGEcommand to obtain the corresponding value in descending order, for example:

> zrevrange kpl 0 -1
1) "eStarPro"
2) "QGhappy"
3) "XQ"
4) "RNG.M"
5) "EDG.M"
6) "TES"
7) "VG"

Plus WITHSCORESparameters, you can return along with the score, such as:

> zrange kpl 0 -1 withscores
 1) "VG"
 2) "2"
 3) "TES"
 4) "4"
 5) "EDG.M"
 6) "8"
 7) "RNG.M"
 8) "8"
 9) "XQ"
10) "10"
11) "QGhappy"
12) "12"
13) "eStarPro"
14) "12"

There are more orderly set of powerful features, such as operating within the range of scores, let's get the team less than 10 (inclusive), use the ZRANGEBYSCOREcommand:

> zrangebyscore kpl -inf 10
1) "VG"
2) "TES"
3) "EDG.M"
4) "RNG.M"
5) "XQ"

This is to get points from minus infinity to a value corresponding to 10, we can get the same value corresponding to the score from 4-10:

> zrangebyscore kpl 4 10
1) "TES"
2) "EDG.M"
3) "RNG.M"
4) "XQ"

Additional useful commands: ZRANKcommands that can return ascending rank specified value (zero); ZREVRANKcommand, which returns the ranked in descending order of the specified value (zero), for example:

> zrank kpl "EDG.M"
(integer) 2
> zrevrank kpl "EDG.M"
(integer) 4

Score ordered set is updated at any time, as long as the collection of the existing order call ZADDcommand, will be to O (log (N)) time complexity updated their score and ranking. Thus, when a large number of updates, ordered set is appropriate. Because of this property, a common scenario is charts can easily display the top N bits of users and user position in the chart.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Hash (Hash)

Redis hash and people expect a "hash" structure is the same, it is an unordered hash, a lot of internal memory key-value pairs, such as:

> hmset one-more-fans:100 name Lily age 25
OK
> hget one-more-fans:100 name
"Lily"
> hget one-more-fans:100 age
"25"
> hgetall one-more-fans:100
1) "name"
2) "Lily"
3) "age"
4) "25"

Although it is easy to hash used to represent objects, but can actually put the number of fields in the hash is no practical limit, so you can more kinds of different ways to use a hash. In addition to HGETcommand gets a value corresponding to a single field may be used HMSETcommand to obtain a plurality of fields and corresponding values, it returns an array, such as:

> hmget one-more-fans:100 name age non-existent-field
1) "Lily"
2) "25"
3) (nil)

You can also use HINCRBYthe command, do the incremental value of the specified field, such as:

> hget one-more-fans:100 age
"25"
> hincrby one-more-fans:100 age 3
(integer) 28
> hget one-more-fans:100 age
"28"

Redis hash implementation structure, and in Java HashMap is the same, but also "array + list" structure, when the collision occurred is an array of position, will string together the elements of a collision with a linked list. But Redis pursuit of high performance, rehash quite the same way, where the first secrecy, the follow-up article will detail.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Bitmap (Bitmap)

Bitmap is not actual data type, but a set of operations defined for the bits in the String. Since the strings are binary safe, and the maximum length is 512MB, it is possible to set up to 2 ^ 32 different bits. Bitmap operations are divided into two categories: fixed single bit operations such as bit set to a 0 or 1 or its value is obtained; operations on groups of bits, such as the number of bits set within the range of the positioning to calculation.

One of the biggest advantages of the bitmap is that they generally can save a lot of space when storing information. For example, a user ID in increments bit identifier indicates whether the user wants to receive newsletters, using only 512 MB of memory can remember an information 40 million users.

Use SETBITand GETBITcommands to set and get designated place, such as:

> setbit one-more-key 10 1
(integer) 0
> getbit one-more-key 10
(integer) 1
> getbit one-more-key 11
(integer) 0

SETBITCommand bit number as its first parameter, which is set to a value of 1 or 0 as its second argument. If the number exceeds the bit length of the current string, the command string will automatically expand. GETBITCommand simply returns the value of a bit position specified number, if the number exceeds the bit length of a string of 0 is returned.

There are three operation command bit group:

  1. BITOPCommand can be executed bitwise, bitwise there is provided between the different strings, or, and a non-exclusive or.
  2. BITCOUNTStatistics command within the specified range median number 1.
  3. BITPOSYou can find the command within the specified range for the first bit 0 or 1.
> set one-more-key "\x13\x7f" #二进制为0001 0011 0111 1111
OK
> bitcount one-more-key #整个字符串中1的位数
(integer) 10
> bitcount one-more-key 0 0 #第一个字符(0001 0011)中1的位数
(integer) 3
> bitcount one-more-key 1 1 #第二个字符(0111 1111)中1的位数
(integer) 7
> bitpos one-more-key 0 #整个字符串中第一个0位
(integer) 0
> bitpos one-more-key 1 #整个字符串中第一个1位
(integer) 3
> bitpos one-more-key 1 0 0 #第一个字符(0001 0011)中第一个1位
(integer) 3
> bitpos one-more-key 1 1 1 #第二个字符(0111 1111)中第一个1位
(integer) 9

Bitmap can be applied to all types of real-time analysis, you can also save space efficiently store bits of information. For example, the user record attendance data every day, every bit indicates whether the user check-ins, so you can calculate a certain period of time the user sign a few times, a period of time the user first sign is the day.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

HyperLogLogs

HyperLogLog is a probabilistic data structure, the number of unique elements for statistical, estimation can be understood as the number of elements in a set.

Often, when statistics for the number of unique elements, requires the use of statistics and the number of elements to be proportional to the amount of memory, because of the need to remember the elements already seen in the past, in order to avoid repeated its statistics. However, there is a set of algorithms can exchange memory precision, ultimately obtained with a standard error of the estimated number of, in the Redis HyperLogLogs, the error is less than 1%.

This algorithm is magic, no longer need to use the number of elements in proportion to the counted amount of memory, but may use a constant amount of memory. Occupies 12KB of memory space in the worst case, Redis of HyperLogLog storage is optimized, counting relatively small, occupying memory space will be smaller, where the first secrecy, the follow-up article will detail where the principle.

In the collection, each element can be added to the collection, and use the SCARDcommand to get the number of elements in the collection, because SADDthe command will not re-add the existing elements, the elements are unique. HyperLogLog similar operation and comparing the set, using the PFADDcommand to add elements to HyperLogLog, similar set of SADDcommands; using the PFCOUNTcommand to get the number of the current approximation HyperLogLog only element, similar to the set SCARDcommand. such as:

> pfadd one-more-hll a b c d e
(integer) 1
> pfcount one-more-hll 
(integer) 5

Redis in HyperLogLog while technically different data structures, but is encoded as a string, it is possible to invoke GETcommands to serialize HyperLogLog, then call SETcommand to deserialize it back to the server.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

to sum up

Redis provides a richer data structure, key (Key) and strings (String), are binary safe string; list (List), according to a list of string elements inserted in sorted order, based on the list implementation; collection (Set) , a collection of strings of elements unique scrambled; ordered set (Sorted set), and a collection of similar, but each element in the string of numbers associated with a score called; hash (the hash), and by the field mapping value associated with the composition, and field values are strings; bitmap (bitmap), the same operation as the operation of the bit array string value, a bit can be set and cleared on all counts the bits 1, find the first a 1 bit set, to find first set bit 0 and the like; HyperLogLogs , a probabilistic data structure using a small memory space to count the number of unique elements, the error is less than 1%.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Guess you like

Origin www.cnblogs.com/heihaozi/p/12164700.html