Redis – set, hash and list

Time:2021-12-24

I List list

The linked list based implementation element is a string type

The addition and deletion at the beginning and end of the list is fast, the addition and deletion in the middle is slow, and the addition and deletion of elements is normal

Elements can be repeated

Contains up to 2 ^ 32-1 elements

 

Index of the list

From left to right, starting from 0

From right to left, starting from – 1

 

1. Press in elements left and right or head and tail

LPUSH key value [value …]

LPUSHX key value

RPUSH key value [value …]

RPUSHX key value

 

2. Left and right or head and tail pop-up elements

LPOP key RPOP key

 

 

 

 

3. Pop up elements from the end of one list and press them into the head of another list

RPOPLPUSH source destination

4. Return the specified range element in the list

Lrange key start stop lrange key 0 – 1 indicates that all elements are returned

5. Get the element at the specified location

LINDEX key index

6. Set the value of the specified location element

LSET key index value

7. List length and number of elements

LLEN key

 

 

 

 

8. Delete the element with value equal to value count times from the list header

LREM key count value

Count > 0: search from the header to the footer, and remove elements equal to value. The number is count

Count < 0: search from the end of the table to the header, remove elements equal to value, and the quantity is the absolute value of count

Count = 0: remove all values equal to value in the table

 

 

 

9. Remove elements outside the specified range

LTRIM key start stop

 

 

 10.Inserts an element before or after an existing value (pivot) in the list

LINSERT key BEFORE|AFTER pivot value

Key and pivot do not exist. No operation is required

 

 

11. Blocking
If the pop-up list does not exist or is empty, it will be blocked
If the timeout is set to 0, it is permanently blocked until there is data to pop up
If multiple clients are blocked on the same list, use the first in first service principle to provide first come first service

Left and right or head and tail blocking pop-up elements

    BLPOP key [key …] timeout

    BRPOP key [key …] timeout

 

II HSH hash

1. Features

  A map key value pair consisting of a field and an associated value

Field and value are string types in a hash

Contains up to 2 ^ 32-1 key value pairs

 

 

2. Set a single field

HSET key field value

HSETNX key field value

Execute when the filed of the key does not exist, and create it directly if the key does not exist

3. Set multiple fields

HMSET key field value [field value …]

4. Return the number of fields

HLEN key

5. Judge whether the field exists

HEXISTS key field

If the key or field does not exist, 0 is returned

 

 

 

 

6. Return field value

HGET key field

7. Return multiple field values

HMGET key field [field …]

8. Return all key value pairs

HGETALL key

9. Return all field names

HKEYS key

10. Return all values

HVALS key

 

 

 

11. Perform incremental calculation of integers on the value corresponding to the field

HINCRBY key field increment

12. Perform incremental calculation of floating point number on the value corresponding to the field

HINCRBYFLOAT key field increment

13. Delete the specified field

HDEL key field [field …]

 

 

 

 

14. Purpose of hash

1) Save memory space

2) Every time a key is created, it will store some additional management information for the key (such as the type of the key, the last time the key was accessed, etc.)

3) Therefore, the more keys in the database, the more memory the redis database server will consume in storing additional management information, and the more CPU it will spend on the management database keys. It will perform incremental floating-point calculation on the value corresponding to the field

 

15. Not suitable for hash

1) Use binary operation commands: redis currently supports setbit, getbit, bitop and other operations on string keys. If you want to use these operations, you can only use string keys, although hash can also save binary data

2) Use expiration key function: redis’s key expiration function can only expire keys, not hash fields. Therefore, if you want to use the expiration function for key value pairs, you can only store key value pairs in the string

 

III Set set

1. Features

Disordered, de duplicated

Element is a string type

Contains up to 2 ^ 32-1 elements

 

 

 

2. Add one or more elements

SADD key member [member …]

If the element already exists, it is automatically ignored

3. Remove one or more elements

SREM key member [member …]

Element does not exist, ignored automatically

4. Return all elements contained in the collection

SMEMBERS key

If there are too many collection elements, such as millions, which need to be traversed, it may cause server blocking, which should be avoided in the production environment

 

 

5. Disorder of sets‘

SADD friends “peter” “jack” “tom” “john” “may” “ben”

SADD anotherfriends “peter” “jack” “tom” “john” “may” “ben”

SMEMBERS friends

SMEMBERS anotherfriends

Note that SMEs may return different results. Therefore, if you need to store ordered and non repetitive data, use an ordered set to store an ordered and repeatable use list

6. Randomly return the specified number in the set

SRANDMEMBER key [count]

If count is positive and less than the set cardinality, the command returns an array of count elements, each of which is different.

If count is greater than or equal to the collection cardinality, the entire collection is returned

If count is negative, the command returns an array. The elements in the array may appear multiple times, and the length of the array is the absolute value of count

If count is 0, null is returned. If count is not specified, a random element is returned

 

 

 

7. Return the number of elements in the collection

The result of the scar key will save the information, and the collection length will be recorded in it, so there is no need to traverse

8. Randomly remove from the collection and return the removed element

SPOP key

 

 

 

9. Difference set

SDIFF key [key …], Remove the intersection of other sets and itself from the set of the first key

SDIFFSTORE destination key [key …], Store the difference result in the target key

 

 

 

 

 

10. Intersection

SINTER key [key …], Take the intersection of all sets

SINTERSTORE destination key [key …], Store the intersection result in the target key

 

 

 

 

 

 

 

 

11. Union

SUNION key [key …], Union of all sets

SUNIONSTORE destination key [key …], Store the union result in the target key

 

 

 

IV Sortedset ordered set

1. Features

Similar to set set

Orderly and de duplication

Element is a string type

Each element is associated with a floating point score, and the elements in the collection are arranged in the order of the score from small to large.

The score can be the same and can contain up to 2 ^ 32-1 elements

 

2. Add one or more elements

ZADD key score member [score member …]

If the element already exists, a new score is used

give an example

Zadd fruits 3.2 bananas

Zadd fruits 2.0 watermelon

Zadd fruits 4.0 guava 7.0 pear 6.8 mango

 

 

3. Remove one or more elements

ZREM key member [member …]

Element does not exist, ignored automatically

give an example

Zrem fruits guava pear mango

Zrem fruits watermelon

 

4. Display score

ZSCORE key member

give an example

Zscore fruits mango

Zscore fruits watermelon

 

The computer can not accurately express every floating-point number. It is an approximate expression

5. Increase or decrease the score

If zincrby key increment member increment is negative, it means decrease

For example, zincrby fruits 1.5 watermelon zincrby fruits – 0.8 banana

 

6. Return the ranking (index) of elements

ZRANK key member

Example: zrank fruits watermelon

Zrank fruits guava

Zrank fruits mango

 

 

 

 

7. Return the reverse ranking of elements

ZREVRANK key member

 

 

 

 

8. Return the element of the specified index interval

ZRANGE key start stop [WITHSCORES]

If the scores are the same, they are arranged according to the lexicographic order. By default, the scores are arranged from small to large. If you need to arrange the scores from large to small, use zrevrange

 

 

9. Return the element of the specified score range

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

The returned score is between [min, Max] by default. The elements are arranged in ascending order according to the score, and the score is in the same dictionary order

In limit, offset represents how many elements are skipped, and count is how many elements are returned. Similar to MySQL

Use parentheses to change the interval to an open interval, such as (5), (10, 5)

-Inf and + inf represent negative infinity and positive infinity

 

 

10. Return the element of the specified score range

ZREVRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

The returned score is between [min, Max] by default, and the elements are arranged in descending order according to the score, which is the same as the descending order of the dictionary

In limit, offset represents how many elements are skipped, and count is how many elements are returned. Similar to MySQL

Use parentheses to change the interval to an open interval, such as (5), (10, 5)

-Inf and + inf represent negative infinity and positive infinity

 

11. Remove the elements of the specified ranking range

 

 

12. Remove the element of the specified score range

 

 

13. Return the number of elements in the set

 

14. Union