当前位置:网站首页>[redis] ② redis general command; Why is redis so fast?; Redis data type
[redis] ② redis general command; Why is redis so fast?; Redis data type
2022-07-26 00:20:00 【Zhangguoqing (Qing Yi)】
Catalog
One 、Redis General Command
Connect Redis client
./redis-cli
./redis-cli -a zgqyyes
- keys *: View all key( Will block other operations , Not recommended )
keys *
- set key value: Set up the data
set name qingyi
- dbsize: see key The number of 【 Very fast performance , Time complexity O(1)】
dbsize
- exists key: Determine if there is a counterpart key( There is returned 1, Otherwise return to 0)
exists name
- expire key seconds: To assign to key Set expiration time ( Company : second )
# Set successfully returned 1
# 5s after name This key Will be deleted
expire name 5
- ttl key: see key How long do you have to live (-1: Permanent validity ;-2: Invalid key )

- persist key: Put a with an expiration date key Change to a permanent key

- del key: Delete specified key, You can delete more than one at a time
# Delete two key: name and sex( The return value is deleted key The number of )
del name sex
- type key: see key Data type of
type name
- flushdb and flushall: Clear database records
- info: View server information
Two 、 Why? Redis It's going to be so fast ?
① Redis Of data yes Stored in memory Of
② Data processing model
③ Avoid the race consumption of context switching ( Single process )
④ Use epoll Model , Non blocking IO
⑤ Redis It's not a single thread ( The execution directory is single threaded )
3、 ... and 、Redis Data type of (*)
Data type refers to Value The type of data stored
The value of the string is the largest 512M
Deposited value Can be a string 、 The number 、 Binary system 、JSON data
(1) String
a. command
value The value of is a string type , It can also be binary data and numbers
command ①:set、get、del
set name pzj
get name
del name
command ②:incr、incrby、decr、decrby( Atomic operation )
The value increases or decreases 1
The value increases or decreases the specified value
# age Of value Reduce 2
decrby age 2
command ③:set、setnx、setxx、setex
set: Add or modify ( If it doesn't exist, add , Modification of existence )
setnx: New operation ( If it exists, it will not be processed )
setxx: Modify the operating ( Modification of existence , If it does not exist, it will not be processed )
setex: Set up key The expiration time of
command ④:mget、mset( Get or set multiple at a time key-value)

command ⑤:getset、append、strlen
getset: Set the value and return the old value
append: String concatenation
strlen: String length
getset name pzj
append name lovezgq
strlen name
command ⑥:incrbyfloat、getrange、setrange
incrbyfloat: Decimal increments , The negative number passed in decreases
getrange: Get some data ( Intercepting string )
settrange: Replace some data
b. Application scenarios
Caching function ( The most classic use scenario of string ):Redis As a cache layer ,MySQL As storage layer . Most of the requested data is in Redis In order to get , because Redis It can support high concurrency , Therefore, caching can usually speed up the reading and writing speed and reduce the back-end pressure
Counter : Many applications use Redis As a counting tool . It enables fast counting 、 The function of querying cached data , At the same time, data can be landed to other data sources step by step . ( Such as : Video playback volume )
share Session: For load balancing , Distributed services will balance the access of user information to different servers , Users may need to log in again for a refresh . To avoid this problem, you can use Redis Will the user Session centralized management . In this mode , Just promise Redis High availability and scalability , Every time you get user updates or query login information, you can directly from Redis To obtain
The speed limit : On safety grounds , Ask the user to enter the mobile phone verification code every time they log in , For short message interface is not frequently accessed , It will limit the frequency of obtaining the verification code per minute


(2) Hash
Hash The type is String Type of field and value Mapping table , Or a String aggregate . It's especially suitable for Store the object , By comparison , Store an object in Hash Type is better than stored in String Type takes up less memory space .
a. command
command ①:hset、hget、hdel
{
handsome:boy1: {
id: 1,
name: pzj,
age: 18
}
}
command ②:hexists( Determine whether the field exists );hlen( Judge how many field Field )
command ③:hmget( Get the value of multiple fields );hmset( Set values for multiple fields )

command ④:hgetall、hvals、hkeys
hgetall: according to key return hash All fields and values of
hvals: according to key Return all values
hkeys: according to key Return all fields 
command ⑤:hsetnx、hincrby、hincrbyfloat
hsetnx: modify field Value of field . If the value exists , No modification ; If it doesn't exist , Modify
hincrby: For fields field Increase or decrease
hincrbyfloat: For fields field Increase or decrease
b. Application scenarios
Hash structure is more intuitive than string serialization cache information , And it's more convenient to update . Often used in User information Management of , but Hash type Different from relational databases , The hash type is sparse Of . And relational databases are completely structured , Relational databases can do complex relational queries , and Redis To simulate complex queries of relational databases , open
It's difficult to send , Maintenance costs are high .
Go to Redis Store user information in :
① Use string type : Modify and add Field trouble
② Use Hash type : You can directly modify a Field Or add a Field
(3) List
It is equivalent to storing array types
- In order
- Can be repeated

Redis in Of List similar Java Medium Queue( queue ), It can also be used as List To use
List Type is a set of linked list structures , Its main functions are push、pop 、 Get elements, etc .Redis Of List The type is Double ended linked list Structure , It can be done through correlation api Add or delete elements at the head or tail of the collection .
lpush: Add elements from the head
lrange fruit 0 -1( From beginning to end )
rpush: Add elements from the tail
lpop: Remove element from header , And return the deleted element
rpop: Remove element from tail , And return the deleted element 
linsert: Go to the front of the specified element (Before) Or behind (After) Insert elements 
lset: Replace the element of the specified index
lrem: Remove elements , Returns the number of deleted elements
lindex: The return name is key Of list in index The element of location
llen: Returns the number of elements 
ltrim: Keep assignments key Elements within the specified range of are not deleted 

(4) Set

command ①:sadd、srem、smembers

command ②:spop
Randomly delete a specified number of elements , And return the value of the deleted element 
sdiff: Return different elements in two sets ( That set is the standard in front of that set )
sdiffstore: Store the different elements returned in another collection
sinter: Returns the intersection of sets
sinterstore: Return the intersection result and store it in another set
sunion: Union and collection
sunionstore: Get Union , Into another set
smove: From a set Move the collection to another set In the assembly ( It's equivalent to cutting and copying )
scard: View the number of elements in the set
sismember: Determine whether an element is an element in the set
srandmember: Returns a random element
(5) ZSet

zadd: Add an element to an ordered collection ( If the element exists, update the order )
zrange: Sort according to the score from small to large
zrevrange: Sort according to the score from big to small
zrem: Delete the name as key Of zset The elements in
zincrby: Automatically increase or decrease with the specified value ( Usage and previous incrby type )
zrangebyscore: Return the data within the specified range
zremrangebyrank: Deletes the element at the specified location
zremrangebysocre: Delete the data of the specified score range
zcard: Returns the number of all elements in the collection
zcount: Go back to the collection score The quantity in a given interval
Ranking List : Orderly collection of classic use scenarios .
For example, video websites need to rank videos uploaded by users . The maintenance of the list may be multifaceted ( According to the time 、 According to the amount of play 、 According to the number of likes you get )
边栏推荐
- 06_ UE4 advanced_ Set up a large map using the terrain tool
- How to use 120 lines of code to realize an interactive and complete drag and drop upload component?
- JSON data development
- 测试7年,面试华为最后面议要薪1万,HR说我不尊重华为,他们没有那么低薪资的岗位~
- SSM environment integration
- Detailed explanation of C language preprocessing
- Matlab makes the image of serial port output data in real time
- CyclicBarrier
- Leetcode high frequency question 66. add one, give you an array to represent numbers, then add one to return the result
- 牛血清蛋白修饰酚酸类及生物碱类小分子/偶联微球的蛋白/牛红细胞SOD的研究
猜你喜欢

拼多多根据ID取商品详情 API 的使用说明

bond网卡模式配置

Elementary C language - branch statements (if, switch)

Binary tree -- 111. Minimum depth of binary tree

Getaverse, a distant bridge to Web3

白蛋白纳米-超声微泡载组织型纤溶酶原激活物基因靶向制备研究

牛血清白蛋白修饰牛红细胞超氧化物歧化酶SOD/叶酸偶联2-ME白蛋白纳米粒的制备

栈的表示和实现(C语言)

白蛋白纳米粒表面修饰低分子量鱼精蛋白LMWP/PEG-1900修饰牛血清白蛋白制备研究

Binary tree - 617. Merge binary tree
随机推荐
[hero planet July training leetcode problem solving daily] 25th tree array
Sorting out the encapsulation classes of control elements in appium
Binary tree -- 104. Maximum depth of binary tree
Binary tree - 404. Sum of left leaves
白蛋白纳米粒表面修饰低分子量鱼精蛋白LMWP/PEG-1900修饰牛血清白蛋白制备研究
MySQL - Multi version concurrency control (mvcc)
牛血清蛋白修饰酚酸类及生物碱类小分子/偶联微球的蛋白/牛红细胞SOD的研究
Yes, UDP protocol can also be used to request DNS server
MySQL——多版本并发控制(MVCC)
Find the intermediate node of the linked list
[英雄星球七月集训LeetCode解题日报] 第25日 树状数组
如何让你的 JS 代码写得更漂亮
Linked list related methods
12. Neural network model
How to make your JS code more beautiful
bond网卡模式配置
获得JD商品详情原数据 API
Hefei approved in advance
Binary tree -- 257. All paths of binary tree
"Demons dance", is the bull market over? 2021-05-13