Table of Contents
What is caching? Why use caching?
1. Basic introduction to Redis (NoSQL)
2. Redis download and installation configuration (Windows, Linux, Ubuntu), Redis-cli (client) basic commands, Redis visualization tool (Redis Desktop Manager)
3. Redis data types and their operation commands
4. Redis security (creating users), data backup and recovery, pipeline technology, partition
5. Redis HyperLogLog, publish subscribe, transaction, script, server command
6. Basic use of Redis (key-value, key-value pairs)
7. Using Redis in traditional Spring
8. Using Redis in Spring Boot
9. Redis master-slave replication (Master/Slave)
10. Reids sentry mode
11. Basic use of Redis distributed and fragmented clusters
Redis HyperLogLog, publish subscription, transaction, script, server command
- Table of contents
- Redis HyperLogLog, publish subscribe, transactions, scripts, server commands
-
- Redis HyperLogLog
- Redis publish subscribe
- Redis transaction
- Redis script
- redis server
Redis HyperLogLog, publish subscription, transaction, script, server command
Redis HyperLogLog
Redis added the HyperLogLog structure in version 2.8.9 for the algorithm of cardinality statistics.
Advantages of HyperLogLog: When the number or volume of input elements is very, very large, the space required to calculate the cardinality is always fixed and small.
In Redis, each HyperLogLog key only needs 12 KB of memory to calculate the cardinality of nearly 2^64 different elements. This is in stark contrast to collections where more elements consume more memory when computing cardinality. However, because HyperLogLog only calculates the cardinality based on the input elements, and does not store the input elements themselves, HyperLogLog cannot return the individual elements of the input like a collection.
What is cardinality?
For example: data set {1, 3, 5, 7, 5, 7, 8}, then the cardinality set of this data set is {1, 3, 5,7, 8}, and the cardinality (non-repeating elements) is 5. Cardinality estimation is to quickly calculate the cardinality within the acceptable range of error
Redis HyperLogLog command
command | description |
---|---|
PFADD key element [element …] | Add the specified element to the HyperLogLog |
PFCOUNT key [key …] | Returns the cardinality estimate for the given HyperLogLog |
PFMERGE destkey sourcekey [sourcekey …] | Merge multiple HyperLogLogs into one HyperLogLog |
Redis publish subscription
Redis publish-subscribe (pub/sub) is a message communication mode: sender (pub) sends messages, subscriber (sub) receives messages.
Redis clients can subscribe to any number of channels
The following figure shows the relationship between the channel channel1 and the three clients subscribed to this channel – client2 , client5 and client1 :
When a new message is sent to the channel channel1 through the PUBLISH command, this message will be sent to the three clients subscribed to it:
Example
The following example demonstrates how publish-subscribe works. In our example we created a subscription channel called redisChat:
redis 127.0.0.1:6379> SUBSCRIBE redisChat Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "redisChat" 3) (integer) 1
Now, first restart a redis client, and then publish two messages on the same channel redisChat, and the subscribers can receive the message.
redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique" (integer) 1 redis 127.0.0.1:6379> PUBLISH redisChat "Learn redis by w3cschool.cn" (integer) 1 #The subscriber's client will display the following message 1) "message" 2) "redisChat" 3) "Redis is a great caching technique" 1) "message" 2) "redisChat" 3) "Learn redis by w3cschool.cn"
Redis publish and subscribe commands
Command | Description |
---|---|
PSUBSCRIBE pattern [pattern …] | Subscribe to one or more channels matching the given pattern |
PUBSUB subcommand [argument [argument …]] | View subscription and publication System status |
PUBLISH channel message | Send information to the specified channel |
PUNSUBSCRIBE [pattern [pattern …]] | Unsubscribe from all channels with the given pattern |
SUBSCRIBE channel [channel …] | Subscribe to the given |
UNSUBSCRIBE [channel [channel …]] | refers to unsubscribe from a given channel |
Redis transactions
Redis transactions can execute multiple commands at once, with the following two important guarantees:
(1) A transaction is a separate isolation operation: all commands in a transaction will be serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by command requests sent by other clients
(2) A transaction is an atomic operation: either all commands in the transaction are executed, or none of them are executed
A transaction goes through the following three stages from start to execution:
①Start business
② command into the team
③Executive affairs
Redis transaction commands
Command | Description |
---|---|
DISCARD | Cancel the transaction and give up executing all commands in the transaction block |
EXEC | Execute all commands in the transaction block |
MULTI | Mark the beginning of a transaction block |
UNWATCH | Cancel WATCH command to monitor all keys |
WATCH key [key …] | Monitor one (or more) key, if the key (or these) is changed by other commands before the transaction is executed , then the transaction will be interrupted |
Redis script
Redis scripts use the Lua interpreter to execute scripts. Reids 2.6 version supports Lua environment through embedding. The common command to execute the script is EVAL
Syntax: EVAL script numkeys key [key …] arg [arg …]
Redis script commands
command | description |
---|---|
EVAL script numkeys key [key …] arg [arg …] | Execute Lua script |
EVALSHA sha1 numkeys key [key …] arg [arg …] | Execute Lua script |
SCRIPT EXISTS script [script …] | Check whether the specified script has been saved in the cache |
SCRIPT FLUSH | Remove all scripts from the script cache |
SCRIPT KILL | Kill the currently running The Lua script |
SCRIPT LOAD script | adds the script script to the script cache, but does not execute the script immediately |
Redis server
Redis server commands are mainly used to manage redis services
Redis server commands
Command | Description | |
---|---|---|
BGREWRITEAOF | Asynchronously execute an AOF (AppendOnly File) file rewrite operation | |
BGSAVE | Asynchronously save the data of the current database to disk in the background | |
CLIENT KILL [ip:port] [ID client-id] | Close client connection | |
CLIENT LIST | Get the list of client connections connected to the server | |
CLIENT GETNAME | Get the name of the connection | |
CLIENT PAUSE timeout | Stop running commands from the client within the specified time | |
CLIENT SETNAME connection-name | Set the name of the current connection | |
CLUSTER SLOTS | Get the mapping array of cluster nodes | |
COMMAND | Get the array of Redis command details | |
COMMAND COUNT | Get the total number of Redis commands | |
COMMAND GETKEYS | Get all keys for a given command | |
TIME | Returns the current server time | |
COMMAND INFO command-name [command-name …] | Get the array of the specified Redis command description | |
CONFIG GET parameter | Get the value of the specified configuration parameter | |
CONFIG REWRITE | For the redis.conf configuration file specified when starting the Redis server Rewrite | |
CONFIG SET parameter value | Modify redis configuration parameters without restarting | |
CONFIG RESETSTAT | Reset some statistics in the INFO command | |
DBSIZE | Return the number of keys in the current database | |
DEBUG OBJECT key | Get key debugging information | |
DEBUG SEGFAULT | Let Redis Service crash | |
FLUSHALL | Delete all keys of all databases | |
FLUSHDB | Delete all keys of the current database | |
INFO [section] | Get various information and statistics of the Redis server | |
LASTSAVE | Returns the last time Redis successfully saved data to disk, in UNIX timestamp format | |
MONITOR | Print out the commands received by the Redis server in real time, use | |
ROLE for debugging | return the role of the master-slave instance | td> |
SAVE | Save data to hard disk asynchronously | |
SHUTDOWN [NOSAVE] [SAVE] | Save data to the hard disk asynchronously, and close the server | |
SLAVEOF host port | Transform the current server into a slave server of the specified server (slave server) | |
SLOWLOG subcommand [argument] | Manage redis slow log | |
SYNC | Internal commands for replication |