当前位置:网站首页>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
边栏推荐
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
- IJCAI2022开会了! Brescia等《证据推理和学习》教程,阐述其最新进展,附96页Slides
- The passwords are consistent, and the total display is as shown in the figure below
- A case study of building an efficient management system for a thousand person food manufacturing enterprise with low code
- MapReduce (II)
- Is it safe for CSC qiniu members to open preferential accounts? I don't know if it's the lowest Commission
- 基于华为云 IOT 设计智能称重系统 (STM32)【二】结尾有资料
- conda转移项目虚拟环境必备技能+pip速度下载太慢解决办法
- 关于接口测试你想知道的都在这儿了
- Data Lake -- concept, characteristics, architecture and case overview
猜你喜欢

聊聊如何用 Redis 实现分布式锁?

Introduce the difference between @getmapping and @postmapping in detail

“蔚来杯“2022牛客暑期多校训练营1

ReentrantLock学习之---基础方法

Aof & RDB of J2 redis

Reentrantlock learning - lock release process

Description of MDM separation of powers and classification and grading authority

Volatile keyword of JVM memory model

Redis6

C # create and read dat file cases
随机推荐
torch. Usage and comparison of unsqueeze() squeeze() expand() repeat()
配置服务器环境
Vs2019 export import configuration
EN 1504-7 products for protection and repair of concrete structures corrosion prevention of reinforcement - CE certification
C # create and read dat file cases
聊聊如何用 Redis 实现分布式锁?
千万不要随便把 Request 传递到异步线程里面 , 有坑 你拿捏不住,得用 startAsync 方法才行
测试人员必须知道的软件流程
Weekend highlights review | establishment of digital RMB industry alliance; China Mobile announced that hefeixin will stop its service
还在用Xshell?你out了,推荐一个更现代的终端连接工具
To add a composite primary key
Still using xshell? You are out. I recommend a more modern terminal connection tool
Is it safe for CSC qiniu members to open preferential accounts? I don't know if it's the lowest Commission
微信小程序插件--wxml-to-canvas(生成图片)
Volatile keyword of JVM memory model
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
Selenium + case of Web Automation Framework
Data Lake -- concept, characteristics, architecture and case overview
Conda+pytorch environment tutorial
EN 1504-7混凝土结构保护和修理用产品钢筋防腐—CE认证