Catalog
One .Redis Data type of
Two .redis data type (5 A common type of )
3、 … and .String type
Four . Numerical operations
5、 … and .String Type extension operation
6、 … and .String Precautions for type data operation
7、 … and .redis The standard way of naming
One .Redis Data type of
1.redis Common solutions Original business function design seckill , Or queue up to buy tickets
2. The operating platform monitors the high frequency burst : Sudden news of current affairs
3. High frequency statistics : Number of people online , Voting charts
4. Additional features : System function optimization or upgrade
Single server upgrade cluster
session management
token management
Two .redis data type (5 A common type of )
redis | java( It’s just similar, but the bottom is not consistent ) |
String | String |
hash | Hashmap |
list | LinkedList |
set | HashSet |
sorted-set | TreeSet |
redis Data storage format
redis Itself is a map, All of the data is in the form of key:value Form storage of
redis Storage space It means value part ,key Values are always strings
3、 … and .String type
1.String type
Store the data : Single data , The simplest type of data storage , It is also the most commonly used data storage type
Storage format : One storage space holds one data
Store content : You usually use strings , If the string is presented as an integer
It can be used as a number , But it’s still in string format
2.String Basic operation of type data
add to / Modifying data
set key value
get data
get key
Delete data
del key
Add modified data
mset key1 value1 key2 value2
Get multiple data
mget key1 key2 key3…..
Get data character length ( String length )
strlen key
Append information to the back of the original message ( If the original information exists, add , Otherwise, we will build a new one )
append key value( Add it to the back if you have , If you have nothing to build )
3. Performance comparison of operation instructions
set key value VS mest key1 value1 key2 value2….
The execution time of an instruction
Single command | 3 Execution time of instructions | 1redis link *6+redis Insert *3 |
Multiple instructions | 3 The execution time of the instruction is | 1redis link *2+redis Insert *3 |
Balance multiple instructions with single instructions
1 Billion bars Multi instruction is better than single instruction , Multiple instructions can cause threads to block Balance thread link time with instruction execution time
Four .String The numerical operation of
String Type of data extension operation
Business scenario :
In large enterprise applications , Sub table operation is a basic operation , Use multiple tables to store data of the same type , But the corresponding primary key id It must be ensured that there is no repetition .oracle The data has sequence Set up , Can solve the problem , but mysql Databases don’t have a similar mechanism , How to solve ?
1. Duplicate primary keys of sub database and sub table
oracle—sequeue
mysql—redis solve the problem
redis Solutions for
Set the numeric data to increase the value of the specified range
incr key Add one to the number stored
incrby key increment Specify how much to add
incrbyfloat key increment Add one decimal at a time
Set the data value to reduce the value of the specified range
decr key
decrby key increment Specifies to decrease by a certain number
Can be negative , So negative is positive
String As a numerical operation
String stay redis Internal storage is a string by default , When the operation of increasing or decreasing numbers is encountered incr,decr It turns into a numerical calculation
redis All operations are Atomicity Of , use Single thread processing all business , Orders are executed one by one , therefore Don’t worry about concurrency The data impact
Be careful : Data that operates by value , If the original data can’t be converted to a numerical value , Or beyond redis The upper range of the upper value , Will be an error
The upper limit is :922337203685477
java Kind of long The maximum value of type data ,LONG.MAX_VALUE
tips:
redis Used to control the database table primary key id, Provide generation strategy for primary key of data table to ensure the uniqueness of primary key
This solution is used for all databases and supports database clustering
5、 … and .String Type extension operation
Business scenario
1.” The strongest girl ” Start voting , You can only vote through wechat , Every micro signal 4 Hours Only one vote
2. E-commerce businesses start to recommend popular products , Hot products can’t be consistent in the hot period , Each item is hot to maintain 3 God ,3 Automatically cancel hot days later
3. Hot news will appear on news websites , The biggest feature of hot news is timeliness , How to automatically control hot news timeliness
redis Set the lifetime
Solution :
Set the data to have a specified lifecycle
setex key seconds value second
psetex key milliseconds value millisecond
tips2:
redis Control the lifecycle of data , Control business behavior by controlling data failure , Applicable to all operations with time limited control
6、 … and .String Precautions for type data operation
1. The successful running —1
Run failed —–0
2. Data not obtained nil Equivalent to Java Of null
3. The maximum storage capacity of data 512M
4. The maximum range of numerical calculation (Java Kind of long The maximum of )
7、 … and .redis The standard way of naming
stay redis Kind of For big V Set user information , Take user primary keys and attributes as key,
Table name : Primary key name : Primary key value : Property value
Distinguish with a colon (:) distinguish , Standard practice
You can also use json The format of the string stores the object , But each one needs to be taken out and modified
tips3:
redis It can be applied to all kinds of structured and unstructured high heat data access acceleration
key Setting conventions for
Table name : Primary key name : Primary key value : Property value A colon generates a namespace
This is the effect of a namespace