Introduction to Redis

Posted by binujayaraj on Fri, 07 Jan 2022 06:40:09 +0100

Redis overview

Redis is a key value storage system (official website: ), 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

docker ps

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. 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 > info # view the detailed configuration information of the current redis node

Clear redis screen

Clear redis screen content> clear

Exit redis service

Exit the redis service, for example> exit

Turn off redis service

Close the redis service, for example:> shutdown

System help

You can view related instruction help based on the help instruction, such as> 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> 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> keys *
(empty array)

Store data in the form of key/value> set test1 123
OK> set test2 ab
OK> keys *
1) "test1"
2) "test2"

Obtain data stored in redis based on key> get test1
"123"> get test2
"ab"> get test3

Clear data in redis

Clear current database data> flushdb

Clear all database data> flushall

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> set bomb tnt OK> expire bomb 10 (integer) 1> ttl bomb (integer) 5> ttl bomb (integer) 3> ttl bomb (integer) 3> ttl bomb (integer) 2> ttl bomb (integer) 1> ttl bomb (integer) -2> ttl bomb (integer) -2>

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> set bomb tnt OK> expire bomb 60 (integer) 1> ttl bomb (integer) 49> persist bomb (integer) 1> ttl bomb (integer) -1>

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> set bomb tnt
OK> pexpire bomb 10000
(integer) 1> ttl bomb
(integer) 6> ttl bomb
(integer) 3> ttl bomb
(integer) -2>

Topics: Database Redis Cache