directory

  • An overview of the
  • Break down

1, an overview of the

Redis supports five data types: String, Hash, List, Set, and Zset.

2, break down,

1, String (String)

Definition: String is the most basic type of redis. Each key corresponds to a value. The String type is binary safe. Because the underlying implementation of string is a simple dynamic string SDS, it is possible to modify the string.

Application scenarios: single-value cache, object cache, distributed lock, general count (number of fans, number of comments), distributed session sharing, distributed global sequence number.

Specific implementation: single value cache:

  set key value
  get key
Copy the code

Object cache:

Set user:1 value(JSON format data) mset user:1:name tsingli user:1:balance 1888 mget user:1:name user:1:balanceCopy the code

Conventional counter:

Incr article:readcount:{article id} get article:readcount:{article id}Copy the code

Distributed system global serial number:

Incrby OrderID 1000 # redis Generates serial numbers in batches to improve performanceCopy the code

Distributed lock:

Thread 1: setnx product:1001 true # return 1: setnx product:1001 true # return 0: setnx product:1001 Del product:1001 # Release lock set product:1001 true ex 10 nx # Prevent deadlock caused by unexpected program termination # case setnx product:1001 true 1 Query the inventory of product 1001. 2. Delete the inventory. 3Copy the code

Setnx does nothing to the data if it inserts the same key and returns an error if it subsets the inventory a second time.

2. Hash

Definition: A Hash is a collection of key-value pairs. It is equivalent to a double map in Java. < key, value > < filed, >. Application scenario: Shopping cart. Specific implementation: Shopping cart:

# user id = 1001, product id = 10088 # user id = key, item id = field, item quantity = value # Add item: hset Cart :1001 10088 1 # Add quantity: hincrby Cart :1001 10088 1 # Add item: hlen Cart :1001 Hdel Cart :1001 10088Copy the code

3. What is the name of the book?

Definition: A list is a simple list of strings, sorted by insertion order, with an element added to the top or bottom of the list. Application Scenarios: Stack = LPUSH + LPOP -> FILO // Queue = LPUSH + RPOP // First in, first out, Blocking MQ = LPUSH + BRPOP // Message queue, weibo, wechat public account message flow. Specific implementation: Microblog and wechat public account message flow:

Lpush MSG :111111 10018 # Lrange MSG :11111 0 5 //0 to 5Copy the code

4. Set

Definition: A Set is an unordered collection of type string. Application scenario: Micro channel lottery small program, micro channel micro blog likes, favorites, labels, attention model, is the relationship of people you may know. Specific implementation: Wechat lottery:

Smembers key # Draw count winners (1) // SrandMember does not remove elements from the collection [count] // Draw two winners srandMember Act :1008 2 # draw count winners (2) // SPOP will remove elements from the set SPOP key 2Copy the code

Wechat and Weibo likes, favorites, tags:

Srem like:{message ID} {user ID} # check whether the user has clicked the "like" button. Sismember like:{message ID} {user ID} # Get the list of users that have clicked the "like" button Smembers like:{ID} scard like:{ID}Copy the code

5. Zset (Ordered set)

Definition: A zset, like a set, is a collection of elements of type string and does not allow duplicate members. The difference is that each element is associated with a score of dobule type. Redis sorts the members of the set from smallest to largest by score. The members of Zset are unique but the score can be repeated.