Redis five data types

  • String, List, Set, Hash, Zset
  • The official documentation

String (String)

Append Appends the string

Syntax: append key value

  • Key exists –> append string
  • Key does not exist –> Create a new one key value, which is equivalent toset key value , but there is a difference: the new key is always in the same placeAfter the first key

Strlen Checks the length of the key

Syntax: strlen key

The incr i++

Syntax: incr key**

  • The value in key must be a number; otherwise, an error message is displayed

Decr since I —

Syntax: decr key

Incrby since I + = step

Syntax: incrby key step

Decrby since I – = step

Syntax: decrby key step

Getrange retrieves the value in the range [a,b]

Syntax: getrange key a B

Setrange replaces value with a string starting at position A

Syntax: setrange key a STR

  • Replace STR as long as it takes
  • Starting at position A, if there is not enough replacement will continue to append by default
  • So starting at position A, if there’s enough substitution, you put in STR, and nothing else.

Setex Sets the value expiration time of a key

Syntax: setex key seconds value

  • TTL key Indicates the remaining time of the key. The value is always -1

setnx set if no exist

Syntax: setnx key value

  • The statement is invalid because the key to be set exists
  • The key to be set does not exist, set the key value

Mset Sets multiple key values simultaneously

Mset k1 v1 k2 v2 k3 v3

Mget gets the value of multiple keys at the same time

Syntax: mget k1 k2 k3

Msetnx sets multiple key values at the same time, but one key is invalid and the other keys are invalid

Syntax: msetnx k1 v1 k2 v2

  • The key to setAt least oneAll keys to be set are invalid —–>atomic
  • None of the keys to be set previously existed –> create key value

Getset returns the original value and then modifies it

Syntax: getSet key value

  • Returns nil if there was no previous value
  • If there is an original value, return the original value, and then modify the original value

Save an object

Syntax: mset Object name: Object value: attribute 1 Value of attribute 1 Object name: Object value: attribute 2 Value of attribute 2…..

  • For objects, here is the design of the key –>Object name: Object value: object property

List (List)

  • List can be thought of as a bidirectional linked List
  • All the orders were given byL(upper case and lower case) with special cases
  • Special case:
    • Rpush right insert
    • Rpop pop up right
    • Rpoplpush pop right and insert left
    • Exists Indicates whether the alarm exists

Lpush stores values to the head of the list (far left)

Syntax: lpush list_name key_value

Rpush exists at the end of the list (far right)

Syntax: rpush list_name key_value

Lrange Displays the values of the list in the interval [A,B]

Syntax: lrange list1 a b

Lpop takes the value of the head (leftmost) of the list, and the list is changed

Syntax: lPOP list_name

Rpop takes the bottom (rightmost) value of the list, and the list is changed

Syntax: rPOP list_name

Lindex gets the value of the index specified in the list

Syntax: lindx list_name index

Llen gets the length of the list

Syntax: llen list_name

Lrem removes specified values and specifies how many to remove

Syntax: lREM list_name rem_number rem_value

  • If the number of rem_values in the list is greater than the number of rem_values in the list, remove all rem_values from the list.

Ltrim truncates the values in [a,b] and changes the list

Syntax: ltrim list_name A b

Rpoplpush removes the rightmost element of the A list and moves it to the B list

Rpoplpush list_name1 list_name2

Lset replaces the value of the index

Syntax: lset list_name index value

  • If the list does not exist, an error is reported

Exists Determines whether the list exists

Syntax: exist list_name

Linsert inserts values into the list

Syntax: linsert list_name before/after old_value new_value

Set

  • Values in a Set are not repeated
  • The Set command toSAt the beginning
  • Set doesn’t have to be non-repetitive, random

Sadd adds values to the Set collection

Syntax: sadd set_name value

Smembers Views all values in a set

Syntax: smembers set_name

Sismember determines whether the current value is in the Set

  • The return value is 1 –> Exists
  • The return value is 0 –> does not exist

Syntax: sismember set_name value

Scard gets the number of values in the set

Syntax: scard set_name

Srem removes the specified element from the set set

Syntax: srem set_name value

Srandmember randomly extracts elements

  • Number is the number of extracted elements. The default value is 1

Syntax: scard set_name (number)

Spop randomly deletes the values in the set collection

  • Number indicates the number of deleted elements. The default value is 1

Grammar:spop set_name (number)

Smove moves a specified value to another set

Syntax: smove set_name new_set_list value

Sdiff The difference between two sets

Syntax: sdiff set_name1 set_name2

The intersection of two sets of sinter

Syntax: sinter set_name1 set_name2

Sunion the union of two sets

Syntax: sunion set_name1 set_name2

Hash (Hash)

  • Think of it as a Map set, key-
    ,value>
  • The original value becomes a map set
  • All orders toHAt the beginning
  • Better for storing objects

Hset Stores fields and field values to the hash table

Syntax: hset key field value

Hget Obtains the value of the corresponding field in the hash table

Syntax: hget key field

Hmset sets multiple fields and their corresponding values at once

Syntax: hmset key filed1 value1 key field2 value2…

Hmget gets the values of multiple fields in multiple hash tables at once

Syntax: hmget key field1 field2

Hexists Determines whether a field exists in the hash table

Syntax: hexists key field

Hgetall Retrieves all fields and field values in the hash table

Syntax: hgetall key

Hkeys retrieves all fields in the hash table

Syntax: hkeys key

Hvals retrieves all field values in the hash table

Syntax: hVALS key

Hlen gets how many fields are in the hash table

Syntax: hlen key

Hdel Deletes the fields of the hash table

Syntax: hdel key field1 (field2….)

Hincrby increments the value of a field in a hash table

  • Number is the added value
  • The fields added must be numbers

Syntax: hincrby key field number

Zset(Ordered set)

  • On the basis of set, a field is addedscore, you can sort by this field
  • scoreIf yes, sort it in lexicographical order

Zadd adds one or more members to an ordered collection, or updates the scores of existing members

Zadd key scoreq member1 score2 member2…

Zcard gets the number of members of the ordered set

Usage: zcard key

Zcount counts the number of members of a specified interval score in an ordered set

Usage: zcount key min Max

The score of a specified member in a zincrby ordered set plus increment N

Zincrby key n member

Zscore returns the score value of a member in an ordered set

Usage: zscore key member

Zrank returns the index of the specified member in the ordered collection

Zrank key member

Zrange returns an ordered collection of the members of a specified range by indexing the range

Usage: zrange key start end

Zrangebylex returns members of an ordered collection through dictionary ranges

Usage: Zrangebylex key min Max

Zrangebyscore returns the members of the ordered set within the specified rangebyscore. -inf and + INF represent the minimum and maximum values, respectively. Only open ranges are supported.

Usage: zrangebyScore key min Max

Zrem removes one or more members of an ordered set

Usage: zrem key member1 member2..

Zrevrange returns the members of an ordered set within a specified interval, indexed from high to bottom

Zrevrange key start end