Official account: Java Xiaokaxiu, website: Javaxks.com

Author: Lucifer_Yu, link: my.oschina.net/u/920698/bl…

background

  • Redis is an open source memory data structure storage system.
  • Can be used as database, cache, and messaging middleware.
  • Support for multiple types of data structures.
  • Redis is built with replication, LUA scripting, LRU eviction, transactions, and different levels of disk persistence.
  • High availability through automatic partitioning of Redis Sentinel and Redis clusters.

Basic data types

  • Strings

1. The expiration time of string is cleared after the value is reset

127.0.0.1:6379> set hello 3
OK
127.0.0.1:6379> get hello
"3"
127.0.0.1:6379> ttl hello
(integer) -1
127.0.0.1:6379> expire hello 3000
(integer) 1
127.0.0.1:6379> set hello 4
OK
127.0.0.1:6379> ttl hello
(integer) -1
Copy the code

2. Set a string value to override any other type

127.0.0.1:6379> sadd settest 1,2 (integer) 1 127.0.0.1:6379> Type settest set 127.0.0.1:6379> Set settest hello OK 127.0.0.1:6379> type settest string 127.0.0.1:6379> sadd settest A, B (error) WRONGTYPE Operation against a key holding the wrong kind of valueCopy the code
  • Hashes
  • Lists

Redis lists are implemented based on Linked Lists. Head and tail operation speed, slow retrieval

  • Sets
  • Sorted sets that support range lookups

The sorting of ordered collections defaults to lexicographical order

  • bitmaps
  • hyperloglogs
  • Support geospatial query by radius index

Application scenarios

string

  • Cache data

Both simple and complex data can be converted directly to string storage.

Key: active: Spring2019 :title value: “2019 Spring Festival Activity “Operation: set

Commodity information, provincial information, activity configuration and a series of cold data cache that does not change often

Very popular data cache, game rankings, background updates every second data

  • Simple counting

Number of participants in 2019 Spring Festival Activities

Key: active: Spring2019: Total Value: 3045 Operation: INCR

  • Time expired

A person can only check in once a day

Key: active: the checkin: userId: a 10000 – day: 20190101 value: check-in timestamp operation: the expire

  • A distributed lock

The following code is not rigorous, nX can be concurrent

127.0.0.1:6379> set lockkey 1  nx
OK
127.0.0.1:6379> set lockkey 1  nx
(nil)
Copy the code

set

  • To weight list

Number of participants in 2019 Spring Festival Activities

Key: active:spring2019:users value: 100010,10020 operations: many

  • The label

User label

  • Merchants label

There are five abcDE tasks in the Spring Festival activity. User A has completed A and B, and user B has completed C and D

  • intersection

User A and user B complete the task

  • And set

Any task completed by user A and user B

  • Difference set

Tasks that user A has not completed

  • Get random elements

Get a random gift from the gift library set

hash

  • Different properties of the same resource

Users won different kinds of prizes during the event

Key: active:spring:g’ifts:user:10010 value: {“giftA”:2,”giftB”:5

Incr can be performed on giftA directly

zset

  • list

User consumption ranking, likes ranking and so on

Key: active: Spring :star:rank value: user ID, score: number of likes Operation: many

Get the top 10 according to the score

Query the score of a user

Query users with scores between 90 and 100

Sometimes our score is not determined by a certain business value, can be sorted by two business value, such as look at the user’s actual score, look at the user level, then we can use before the decimal point in design when the score values mean score, the level of value after the decimal point, if there are other special requirements, You can also think about the score plus some maximum.

Matters needing attention

  • Each key should have a reasonable expiration time
  • The expiration time of string will be overwritten after the value is reset
  • A string set operation can override a type
  • Use appropriate data structures

Don’t use lists to store and retrieve large amounts of data

  • Plan the number of keys properly

Use set instead of a key for each user

  • Environmental data isolation
  • Business data isolation User RedIS business Redis activity Redis should be distinguished and the activity redis can be cleaned freely after the activity
  • Use pipes, Lua scripts, and Redis transactions wisely to improve performance, especially when using Redis in scripts
  • On Reids online systems with a large number of keys, disable the keys * operation in the main library to prevent jams