An overview of the

Redis supports five data types: String, Hash, List, Set, and Zset. This paper introduces the usage of these five data types in detail. This article only lists the basic commands in the command introduction section. For details about how to use these commands, please refer to the Redis official documentation: Redis Commands

String type

String is the most basic data type in Redis. It can store any type of string, including binary data. Can be used to store mailboxes, jsonized objects, or even a picture. A string allows up to 512MB of storage. Strings are the basis of the other four types, and the difference from them is essentially just the way strings are organized.

Basic commands

String operations

  1. SETAssignment, usage:SET key value
  2. GETValue, usage:GET key
  3. INCRIncrementing digits, only for keys of numeric type, equivalent to the Java i++ operation.INCR key
  4. INCRBYIncrements the specified number, useful only for numeric keys, equivalent to Java I +=3.INCRBY key incrementIncrement increment is the key increment.
  5. DECRDecrement numbers, useful only for keys of numeric type, equivalent to Java I -.DECR key
  6. DECRBYReduces the number specified, only for numeric keys, equivalent to Java I -=3.DECRBY key decrement, which means that the key decreases decrement. Decrement can be positive, indicating an increase.
  7. INCRBYFLOATIncrements the specified floating-point number, useful only for numeric keys. Usage:INCRBYFLOAT key increment
  8. APPENDAppend (” world “) to the end, equivalent to “hello” in Java.APPEND key value
  9. STRLENGets the length of the string.STRLEN key
  10. MSETSet multiple key values at the same time.MSET key1 value1 [key2 value2 ...]
  11. MGETObtain multiple keys at the same time.MGET key1 [key2 ...]

Bit operation

  1. GETBITGets the value (0/1) at the specified position of a key’s binary bit.GETBIT key offset
  2. SETBITSets the value (0/1) at the specified position of a key bit.SETBIT key offset value
  3. BITCOUNTThe number of 1’s in a range of binary representations of a key.BITCOUNT key [start end]
  4. BITOPThis command can perform bitoperations on multiple string keys and store the result in the specified key. The operations supported by BITOP include:OR,AND,XOR,NOTUsage:BITOP OP desKey key1 key2
  5. BITPOSGets the position where the first bit of the specified key is 0 or 1.BITPOS key 0/1 [start, end]

Hash type

A hash type is equivalent to a HashMap in Java. Its value is a dictionary that holds many pairs of keys and values. Each pair of keys and values is of a string type. A hash type key can contain up to 2 ^ 32 -1 fields.

Basic commands

  1. HSETAssignment, usage:HSET key field value
  2. HMSETTo assign more than one field at a time.HMSET key field1 value1 [field2 values]
  3. HGETValue, usage:HGet key field
  4. HMGETTake the value of more than one field at a time.HMGet key field1 [field2]
  5. HGETALLTake the values of all fields at once.HGETALL key
  6. HEXISTSCheck whether the field exists.HEXISTS key field
  7. HSETNXIf a field does not exist, use:HSETNX key field value
  8. HINCRBYIncrementing numbers, only for values of numeric type. Usage:HINCRBY key field increment
  9. HDELDelete field, usage:HDEL key field
  10. HKEYSGet all field names, usage:HKEYS key
  11. HVALSGet all field values, usage:HVALS key
  12. HLENGet the number of fields, usage:HLEN key

List the type

The list type is used to store an ordered list of strings, usually by adding elements to both ends of the queue or retrieving a fragment of the list. Internally, the list is implemented using a double Linked list, so the time to add elements to both ends of the list is O(1), and the closer you get to both ends of the list, the faster you get. The downside is that using a list to access an element through an index is inefficient (you need to traverse the element starting at the endpoint). Therefore, the use scenario of the list is generally like: news in moments, and only care about the latest content. With list types, Redis can also be used as a message queue.

Basic commands

  1. LPUSHAdd an element to the left of a list.LPUSH key value
  2. RPUSHAdd an element to the right end of a list.RPUSH key value
  3. LPOPEject an element from the left of the list.LPOP key
  4. RPOPEject an element from the right of the list.RPOP key
  5. LLENGet the number of elements in the list.LLEN key
  6. LRANGEGets the element of a segment in the list.LRANGE key start stopIndex starts at 0 and -1 represents the last element
  7. LREMRemoves the specified value from the list.LREM key count valueIf count>0, all elements whose value is value will be deleted. If count<0, all elements whose value is value will be deleted
  8. LINDEXGets the element value of the specified index.LINDEX key index
  9. LSETSets the element value of the specified index.LSET key index value
  10. LTRIMOnly the list specified fragment is reserved.LTRIM key start stop, including start and stop
  11. LINSERTInsert an element as in a listLINSERT key BEFORE|AFTER privot value, looking from the left for the first element with a value of privot, and then inserting a value BEFORE or AFTER the element, depending on whether the second argument is BEFORE or AFTER
  12. RPOPLPUSHTo escape an element from one list to another.RPOPLPUSH source destination

The Set type

The concept of set is learned in high school textbooks. Every element in a set is different. The maximum number of elements in a set is 2 ^ 32 -1, and there is no order of elements in a set.

Basic commands

  1. SADDTo add an element, use:SADD key value1 [value2 value3 ...]
  2. SREMDelete an element.SREM key value2 [value2 value3 ...]
  3. SMEMBERSGet all the elements in the set.SMEMBERS key
  4. SISMEMBERDetermine if an element is in a set.SISMEMBER key value
  5. SDIFFTo perform a difference set operation on a set.SDIFF key1 key2 [key3 ...], first calculate the difference set of key1 and key2, then use the result to do the difference set with key3
  6. SINTERTo perform an intersection operation on a set.SINTER key1 key2 [key3 ...]
  7. SUNIONTo perform a union operation on a set.SUNION key1 key2 [key3 ...]
  8. SCARDGet the number of elements in a set.SCARD key
  9. SDIFFSTOREMake a difference set of a set and store the result.SDIFFSTORE destination key1 key2 [key3 ...]
  10. SINTERSTORETo perform an intersection operation on a set and store the result. Usage:SINTERSTORE destination key1 key2 [key3 ...]
  11. SUNIONSTORETo perform a union operation on a set and store the result. Usage:SUNIONSTORE destination key1 key2 [key3 ...]
  12. SRANDMEMBERGet the elements of a collection randomly.SRANDMEMBER key [count]When the count > 0, random access to count a not repeating elements in the collection, when the count < 0, random access in the collection | count | and may duplicate elements.
  13. SPOPTo pop an element randomly from a collection.SPOP key

ZSet type (SortSet)

The difference between an ordered set type and a set type is that it is ordered. Ordered sets associate each element with a score based on the set, which allows ordered sets not only to insert, delete, and determine the existence of elements, but also to get the first N elements with the highest/lowest score. Each element in an ordered set is different, but the fractions can be the same. Ordered sets are implemented using hash tables and skip lists. Even reading the data in the middle is fast and the time complexity is O(log(N)). Ordered sets consume more memory than lists.

Basic commands

  1. ZADDTo add an element, use:ZADD key score1 value1 [score2 value2 score3 value3 ...]
  2. ZSCOREGet the score of an element.ZSCORE key value
  3. ZRANGEGets elements ranked in a range.ZRANGE key start stop [WITHSCORE], elements are sorted from smallest to largest, numbered from 0, including elements corresponding to start and stop. The WITHSCORE option indicates whether to return element scores
  4. ZREVRANGEGets elements ranked in a range.ZREVRANGE key start stop [WITHSCORE], the same as the previous command, but in reverse order.
  5. ZRANGEBYSCOREGets the elements within the specified score range.ZRANGEBYSCORE key min max, including min and Max,(minIndicates that min is not included,(maxMax is not included,+infInfinity
  6. ZINCRBYTo increase the score of an element.ZINCRBY key increment value
  7. ZCARDGets the number of elements in a set.ZCARD key
  8. ZCOUNTGets the number of elements in the specified score range.ZCOUNT key min maxMin and Max are used in the same way as in 5
  9. ZREMTo remove one or more elements.ZREM key value1 [value2 ...]
  10. ZREMRANGEBYRANKDelete elements by rank range.ZREMRANGEBYRANK key start stop
  11. ZREMRANGEBYSCOREDelete elements according to the score range.ZREMRANGEBYSCORE key min maxMin and Max are used in the same way as in 4
  12. ZRANKGets the rank of the ordered elements.ZRANK key value
  13. ZREVRANKGets the rank of the elements in reverse order.ZREVRANK key value
  14. ZINTERSTOREComputes the intersection of ordered sets and stores the result. Usage:ZINTERSTORE destination numbers key1 key2 [key3 key4 ...] WEIGHTS weight1 weight2 [weight3 weight4 ...] AGGREGATE SUM | MIN | MAX, “numbers” indicates the number of sets participating in the operation, “weight” indicates the weight, and “aggregate” indicates the value of the result
  15. ZUNIONSTORE computes the union of ordered several and stores the result. The usage is the same as 14.

The statement

Qifuguang.me /2015/09/29/…