Redis data structure

1, the strings

set mystr "hello world!" // Sets the string type
get mystr // Reads the string type
Copy the code

You can also perform numeric manipulation on string types

127.0. 01.:6379> set mynum "2"
OK
127.0. 01.:6379> get mynum
"2"
127.0. 01.:6379> incr mynum
(integer) 3
127.0.0.1:6379> get mynum
"3"
Copy the code

When a numeric operation is encountered, Redis converts the string type to a numeric value. Since INCR and other instructions themselves have the characteristics of atomic operation, we can completely use redis INCR, INCRBY, DECR, DECRBY and other instructions to achieve the effect of atomic counting. If, in a certain scene, three clients simultaneously read the value of mynum (the value is 2), If I increment both of them by 1, mynum must be 5. Many websites use this feature of Redis to achieve business statistics requirements. If multiple customers execute setnx at the same time, only one can be set successfully. The distributed lock set command also supports batch processing, Mset/MGET batch setting and batch obtaining. Batch operations improve execution efficiency. Otherwise, n requests need to be sent for n queries in a batch.

2. Hash

Hash stores mappings between strings and string valuesEspecially suitable forStore the object, such as aHashing is good for users who want to store their full name, last name, age, and so on(Using String here takes up too much key space).

  • hset key field valueA single assignment
  • hmset key field value [filed value]...Batch assignment
// Create the hash and assign the value
127.0. 01.:6379> HMSET user:001 username antirez password P1pp0 age 34 
OK

// List the contents of the hash
127.0. 01.:6379> HGETALL user:001 
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "34"

// Change a value in the hash
127.0. 01.:6379> HSET user:001 password 12345 
(integer) 0

// List the hash contents again
127.0. 01.:6379> HGETALL user:001 
1) "username"
2) "antirez"
3) "password"
4) "12345"
5) "age"
6) "34"
Copy the code

In general application scenarios, there are three solutions to cache user information: 1. Set storage; 2. Object information serialization storage; 3. Hset storage.

3, Lists set

First of all, it should be clear that lists in Redis are not arrays in the underlying implementation, but linked lists, so it is fast to add, delete and change, and slow to locate. Common operations on Lists include LPUSH, RPUSH and LRANGE. We can use LPUSH to insert a new element to the left of lists, RPUSH to insert a new element to the right of lists, and LRANGE to specify a range from lists to extract elements.

// Create a new list called myList and insert the element "1" at the head of the list.
127.0. 01.:6379> lpush mylist "1" 
// Returns the number of elements in myList
(integer) 1 
// Insert element "2" to the right of myList
127.0. 01.:6379> rpush mylist "2" 
(integer) 2
// Insert element "0" to the left of myList
127.0. 01.:6379> lpush mylist "0" 
(integer) 3
// List the elements numbered 0 through 1 in myList
127.0. 01.:6379> lrange mylist 0 1 
1) "0"
2) "1"
// List the elements numbered from 0 to the last in myList
127.0. 01.:6379> lrange mylist 0 -1 
1) "0"
2) "1"
3) "2"
Copy the code

4, set unordered set

The set of Redis is a kind of unordered set, and the elements in the set have no order. Collection related operations are also very rich, such as adding new elements, deleting existing elements, fetching intersection, fetching union, fetching difference set, etc.

// Add a new element "one" to mySet
127.0. 01.:6379> sadd myset "one" 
(integer) 1
127.0. 01.:6379> sadd myset "two"
(integer) 1
// Lists all the elements in the collection mySet
127.0. 01.:6379> smembers myset 
1) "one"
2) "two"
// Check whether element 1 exists in myset
127.0. 01.:6379> sismember myset "one" 
(integer) 1
// Check whether element 3 is in myset. Return 0 to indicate that it does not exist
127.0. 01.:6379> sismember myset "three" 
(integer) 0
// Create a new collection yourSet
127.0. 01.:6379> sadd yourset "1" 
(integer) 1
127.0. 01.:6379> sadd yourset "2"
(integer) 1
127.0. 01.:6379> smembers yourset
1) "1"
2) "2"
// Find the union of two sets
127.0. 01.:6379> sunion myset yourset 
1) "1"
2) "one"
3) "2"
4) "two"
Copy the code

Usage scenarios: tag, social contact, search for people with common interests and hobbies, intelligent recommendation

5. Zset ordered set

Like unordered collections, strings are stored and cannot be repeated. Each element is associated with a double score. Redis uses scores to sort the members of a collection from smallest to largest.

redis 127.0. 01.:6379> ZADD key 1 redis
(integer) 1
redis 127.0. 01.:6379> ZADD key 2 mongodb
(integer) 1
redis 127.0. 01.:6379> ZADD key 3 mysql
(integer) 1
redis 127.0. 01.:6379> ZADD key 3 mysql
(integer) 0
redis 127.0. 01.:6379> ZADD key 4 mysql
(integer) 0
redis 127.0. 01.:6379> ZRANGE key 0 10 WITHSCORES
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"
Copy the code

Often used for ranking, such as video websites need to do ranking of videos uploaded by users, or the number of likes

Summarize data type usage scenarios

Type string

  1. Cache structure information (mostly used to cache JSON)
  2. Various verification codes (with expiration time)
  3. The count function
  4. Caches sessions and tokens

Hash type

  1. This is especially good for holding object structure information

List (bottom linked list)

  1. All kinds of list
  2. Message queue (sequential consumption)

Set In Java, the internal keys of a HashSet are unordered unique

  1. Provides a list of quick deduplicates
  2. Provides an important interface for determining whether a member is in a set that a list does not provide.
  3. Tag, social contact, search people with common interests, intelligent recommendation to do the collection of the operation

Sorted Set Sorts a collection

A scenario where the data needs to be sorted according to a weight. For example, in the live broadcast system, the real-time ranking information includes the list of online users in the live broadcast room, the list of all kinds of gifts, and the bullet-screen message (which can be understood as the news ranking by the message dimension).