当前位置:网站首页>Redis introduction
Redis introduction
2022-07-26 19:38:00 【No, no, no, no】
List of articles
One 、Redis Introduce
Redis It's based on key value pairs (key-value) In-memory database , among value It can be for string、hash、list、set、zset And other data structures , It can satisfy many application scenarios . Key expiration is also provided 、 Publish subscribe 、 Business 、 Pipeline and other additional functions .
characteristic
1) Fast
Official read / write performance 10 ten thousand /s( It's about machine performance ).
The main reason for the speed :
a) Data is stored in memory
b) C Language implementation , Close to the operating system
c) A single threaded architecture is used , Prevent possible contention problems caused by multithreading
2) Data structure of key value pair
3) Rich functions
4) Simple and stable , Single thread
5) Persistence
Data persistence to hard disk
6) Master slave copy
Realize multiple copies of the same data
7) Highly available and distributed
Sentinel mechanism to achieve high availability , Guarantee redis Node fault detection and automatic transfer .
8) The client supports multiple languages
Support java、php、python、c、c++、nodejs etc. .
Use scenarios
1) Cache database
2) Distributed lock
3) Publish subscribe
4) Ranking List
5) Counter application
6) Social networks
7) Current limiting
Two 、Redis data structure
2.1 character string (String)
It can actually be a string ( Include XML JSON)、 Numbers 、 Base number ( picture Audio video ), Maximum not exceeding 512MB.
Set value command
SET key value [EX seconds]
MSET key value [key value ...]
Value command
GET key
MGET key [key ...]
Count command
INCR key // Integral plus 1, Non integer return error .key Create if it does not exist , And back to 1.
DECR key; // reduce 1
INCRBY key increment // Increase the given value
DECRBY key increment // Subtract the given value
INCRBYFLOAT key increment // Increase the given floating-point value
Other commands
APPEND key value // Append string
STRLEN key // Get string length
GETRANGE key start end // Intercepting string
2.2 Hash (hash)
hash It's a string Type of field and value Mapping table , Suitable for storing objects .
Common command
HSET key field value [field value ...]
HMSET key field value [field value ...]
HGET key field
HMGET key field [field ...]
HDEL key field [field ...]
HLEN key // return field The number of
HEXISTS key field // Judge field Whether there is
HKEYS key // Get all field
HVALS key // Get all value
HGETALL key // Get all field and value
HINCRBY key field increment
HINCRBYFLOAT key field increment
2.3 list (list)
Used to store multiple ordered strings , A list can save up to 232 - 1 Elements .
Add command
RPUSH key element [element ...] // Add from right
LPUSH key element [element ...] // Add... From the left
LINSERT key BEFORE|AFTER pivot element // Insert before or after the specified element .
Search for commands
LRANGE key start stop
LINDEX key index // Get elements by subscript
The delete command
LPOP key [count] // Delete the specified number of elements from the left
RPOP key [count] // Delete the specified number of elements from the right
LREM key count element // remove count Specified elements ,count>0 Remove from left to right ;count<0 Remove from right to left ,count=0 Remove all .
LTRIM key start stop // Delete elements outside the specified range
Modify the order
LSET key index element // Modify the specified subscript element
Other commands
LLEN key // Get list size
2.4 aggregate (set)
Add command
SADD key member [member ...]
Search for commands
SMEMBERS key // Return all elements
SRANDMEMBER key [count] // Random return count Elements
SCARD key // Returns the number of collection elements
The delete command
SREM key member [member ...] // Remove elements
SPOP key [count] // Randomly delete and return count Elements
Other commands
SISMEMBER key member // Determine whether there are elements in the set
SINTER key [key ...] // Returns the intersection of sets
SUNION key [key ...] // Returns the union of sets
SDIFF key [key ...] // Returns the difference set of a set
2.5 Ordered set (zset)
ZADD key score member [score member ...] // Add or modify
ZRANGE key min max [WITHSCORES] // Check out the elements
ZREVRANGE key min max [WITHSCORES] // Check out the elements
ZSCORE key member // View element scores
ZINCRBY key increment member // Elements increase scores
ZCARD key // Count the number of elements
ZCOUNT key min max // Returns the number of elements within the specified score range
ZREM key member [member ...] // Remove elements
ZREMRANGEBYSCORE key min max // Delete elements in the specified score range
ZRANK key member // Returns the subscript of the element in positive order according to the fraction
ZREVRANK key member // Returns the subscript of the element flashback according to the score
ZRANGEBYSCORE key min max [WITHSCORES] // Returns the elements within the specified score range , positive sequence
ZREVRANGEBYSCORE key min max [WITHSCORES] // Returns the elements within the specified score range , In reverse order
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] // Find the intersection
3、 ... and 、 Redis Persistence
Redis Is a memory database that supports persistence , Persistence can avoid data loss caused by process exit . There are ways to persist RDB、AOF Two kinds of .
RDB(Redis DataBase)
It refers to writing the data set snapshot in memory to disk within a specified time interval . It's also the default way to persist .
RDB The trigger methods are :
1) save command
This command will block the current Redis, perform save During the order ,Redis Can't handle other orders . If the memory instance is more likely to cause long-term blocking , It is not recommended for online environment .
2) bgsave command
Redis Process execution Action create subprocess , Persistence is done by child processes . The blockage only happens in fork Stage , Time is very short ( Subtlety ). Basically Redis Internally owned RDB The operation is to use bgsave command .
3) Automatic triggering
Automatically trigger according to the configuration file .
advantage :
1) Compressed binary text , For backup 、 Copy in full , For disaster recovery .
2) load RDB Recover data much faster than AOF The way .
shortcoming :
1) Unable to achieve real-time persistence , Create subprocesses every time , Frequent operation cost is too high .
2) Saved binary , The old version is not compatible with the new version rdb Problems with documents .
3) Since the child process is started for persistence , There may be a small amount of data loss .
AOF(Append Only File)
Redis Will pass every written order received write Function appended to file . The common sense is to log .
AOF technological process :
1) All write commands (set hset) Will add (append) To aof_buf Buffer zone .
2) AOF The buffer synchronizes with the hard disk (sync)
3) With AOF The files are getting bigger , Need to check AOF File rewriting (rewrite), Compression reached .
4) When redis Service to restart , can load load AOF File for recovery .
AOF Trigger mode :
1)always
Synchronous operation , Every time there is a data change, it will be recorded to the disk immediately , Poor performance but good data integrity .
2)everysec
Asynchronous operations , Record every second If it goes down in a second , There is data loss
3)no
Fully dependent on the operating system , Best performance , Persistence cannot be guaranteed ( Synchronization of the operating system itself ).
advantage :
1) AOF Better protect data from loss .
2) AOF The log file does not have any disk addressing overhead , Very high write performance , Documents are not easy to break .
3) AOF Even when the log file is too large , Background override operation occurred , It will not affect the client's reading and writing .
shortcoming :
1) For the same data ,AOF Log files are usually larger than RDB Larger data snapshot file .
2) AOF After opening , Supporting writing QPS than RDB Supporting writing QPS low .
Four 、Redis Publish subscribe
Redis Publish subscribe (pub/sub) It's a message communication mode : sender (pub) Send a message , subscriber (sub) receive messages .Redis Clients can subscribe to any number of channels .
first redis-cli client
redis 127.0.0.1:6379> SUBSCRIBE chat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "runoobChat"
3) (integer) 1
the second redis-cli client
redis 127.0.0.1:6379> PUBLISH chat"Redis PUBLISH test"
(integer) 1
redis 127.0.0.1:6379> PUBLISH chat"Learn redis by runoob.com"
(integer) 1
# The subscriber's client will display the following message
1) "message"
2) "chat"
3) "Redis PUBLISH test"
1) "message"
2) "chat"
3) "Learn redis by runoob.com"
5、 ... and 、Redis Business
In depth understanding of Redis Business
6、 ... and 、Redis Distributed lock
边栏推荐
- EN 1504-7 products for protection and repair of concrete structures corrosion prevention of reinforcement - CE certification
- Difficult performance problems solved in those years -- ext4 defragmentation
- Using MySQL master-slave replication delay to save erroneously deleted data
- The role of @requestmapping in the project and how to use it
- ReentrantLock学习之---基础方法
- Last blog post
- J2 Redis之 AOF&RDB
- Is it safe for CSC qiniu members to open preferential accounts? I don't know if it's the lowest Commission
- "Weilai Cup" 2022 Niuke summer multi school training camp 1
- How to solve the problem that win11 has been switched on after upgrading
猜你喜欢

J1:Redis为什么这么快+基本结构

Advantages of advanced anti DDoS IP in Hong Kong and which industries are suitable for use

How to protect e-commerce websites from network attacks?

博客维护记录之图片预览嵌入位置问题

Introduce the difference between @getmapping and @postmapping in detail

ReentrantLock学习之---释放锁过程

conda+pytorch环境教程

"Weilai Cup" 2022 Niuke summer multi school training camp 1

J3: redis master-slave replication

Still using xshell? You are out. I recommend a more modern terminal connection tool
随机推荐
A case study of building an efficient management system for a thousand person food manufacturing enterprise with low code
测试面试题集-UI自动化测试
The passwords are consistent, and the total display is as shown in the figure below
Synchronized理论
EN 1504-6混凝土结构保护和修理用产品钢筋锚固—CE认证
第九章 实用建模技术
MySQL主从复制配置详解
Network protocol: tcp/ip protocol
Reentrantlock learning --- basic method
C语言-入门-语法-字符串(十一)
周末看点回顾|数字人民币产业联盟成立;中国移动宣布:和飞信将停止服务…
节省50%成本 京东云发布新一代混合CDN产品
If the key is forgotten and multiple devices have different keys, how does the cloud synchronize
PADS画2.54mm排针
[server data recovery] data recovery case of server storage shared folder loss
Reentrantlock learning - Basic Attributes
PyQt5快速开发与实战 3.6 打包资源文件
Typescript stage learning
测试人员必须知道的软件流程
The role of @requestmapping in the project and how to use it