Redis is a key value storage system (official website: http://redis.io ), is a distributed cache database
Redis initial operation
Start redis service
Startup in docker environment (multiple containers need to be run to start docker environment):
The # bottom layer of docker start redis01 is also started through redis server. Redis01 after the word start is the container name
View redis service in docker
View the started redis process information
ps -ef|grep redis
Enter redis container
docker exec -it redis01 bash #redis01 is the container name
Log in to redis service
Log in to local Redis
Redis cli or redis cli - P 6379 or redis cli - P 6379 - a password
#-a is followed by password. Redis.com needs to be enabled for this operation requirepass option in conf file
Log in to remote Redis
redis-cli -h ip -p 6379 -a password
View redis information
First log in to redis, and then enter the info command, such as
127.0.0.1:6379 > info # view the detailed configuration information of the current redis node
Clear redis screen
Clear redis screen content
Exit redis service
Exit the redis service, for example
Turn off redis service
Close the redis service, for example:
You can view related instruction help based on the help instruction, such as
127.0.0.1:6379> help redis-cli 2.8.19 Type: "help @<group>" to get a list of commands in <group> "help <command>" for help on <command> "help <tab>" to get a list of possible help topics "quit" to exit
127.0.0.1:6379> help type TYPE key summary: Determine the type stored at key since: 1.0.0 group: generic
Redis data storage
Simple data access
View key s in redis based on
127.0.0.1:6379> keys * (empty array)
Store data in the form of key/value
127.0.0.1:6379> set test1 123 OK 127.0.0.1:6379> set test2 ab OK 127.0.0.1:6379> keys * 1) "test1" 2) "test2"
Obtain data stored in redis based on key
127.0.0.1:6379> get test1 "123" 127.0.0.1:6379> get test2 "ab" 127.0.0.1:6379> get test3 (nil) 127.0.0.1:6379>
Clear data in redis
Clear current database data
127.0.0.1:6379> flushdb OK
Clear all database data
127.0.0.1:6379> flushall OK
Key effective time design
In practice, we often need to control the effective duration of key s in redis, such as the timing of second kill operations, the effective duration of cached data, etc.
Expire (setting effective time - unit: seconds)
Syntax: exit key seconds
127.0.0.1:6379> set bomb tnt OK 127.0.0.1:6379> expire bomb 10 (integer) 1 127.0.0.1:6379> ttl bomb (integer) 5 127.0.0.1:6379> ttl bomb (integer) 3 127.0.0.1:6379> ttl bomb (integer) 3 127.0.0.1:6379> ttl bomb (integer) 2 127.0.0.1:6379> ttl bomb (integer) 1 127.0.0.1:6379> ttl bomb (integer) -2 127.0.0.1:6379> ttl bomb (integer) -2 127.0.0.1:6379>
Among them, TTL views the remaining time of the key. When the return value is - 2, it indicates that the key is deleted.
When the key does not exist, - 2 is returned. When the key exists but the remaining lifetime is not set, - 1 is returned.
Persist (cancel duration setting)
Invalidate the effective duration of a specific key setting through persist.
Syntax: PERSIST key
127.0.0.1:6379> set bomb tnt OK 127.0.0.1:6379> expire bomb 60 (integer) 1 127.0.0.1:6379> ttl bomb (integer) 49 127.0.0.1:6379> persist bomb (integer) 1 127.0.0.1:6379> ttl bomb (integer) -1 127.0.0.1:6379>
When setting new data, you need to reset the lifetime of the key, and resetting the value will also clear the lifetime.
pexpire (in milliseconds)
pexpire allows the effective duration of the key to be measured in milliseconds, which can achieve more accurate time control. For example, it can be applied to a second kill scenario.
Syntax: PEXPIRE key milliseconds
127.0.0.1:6379> set bomb tnt OK 127.0.0.1:6379> pexpire bomb 10000 (integer) 1 127.0.0.1:6379> ttl bomb (integer) 6 127.0.0.1:6379> ttl bomb (integer) 3 127.0.0.1:6379> ttl bomb (integer) -2 127.0.0.1:6379>