Some operations of redis

redis installation

Ubuntu installation

If you have root permissions or can use sudo to install, it is very simple:
Update the system

sudo apt update

apt installation

sudo apt install redis-server

After the installation is complete, test the Redis version and installation:

redis-cli --version

or

wget http://download.redis.io/releases/redis-7.0.9.tar.gz

tar -zxvf redis-7.0.9.tar.gz

sudo mv ./redis-7.0.9  /usr/local/

cd /usr/local/redis-7.0.9
sudo make

sudo make test

sudo make install
#执行完成后,将redis的可执行程序安装在/usr/local/bin中

Go to /usr/local/bin to see if it is installed

cd /usr/local/bin

ls -all

redis-server       #redis的服务器
redis-cli       #redis命令行客户端
redis-benchmark  #redis性能测试工具
redis-check-aof  #AOF文件修复工具
redis-check-rdb        #RDB文件检索工具

If you do not have root permissions, you cannot use sudo to install, and you need to install it locally.

wget http://download.redis.io/releases/redis-7.0.9.tar.gz
tar -zxvf redis-7.0.9.tar.gz
cd redis-7.0.9
sudo make
sudo make test

Insert image description here
Create a new redis folder and install redis as follows:

make install PREFIX=../redis

Win11 installation

Clear current cache and all caches

Using the command line to clean redis cache in Windows environment

  • Enter cmd in the redis installation directory
  • redis-cli -p port number
  • flushdb clears the current database cache
  • flushall clears all redis caches

In the redis installation directory, right-click and select "Open in Terminal", as shown below:
Insert image description here

(base) PS C:\Program Files\Redis> redis-cli -p 6379
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> flushall
OK
127.0.0.1:6379>

Insert image description here

Configure memory size to prevent memory from becoming full

1. Set the memory size through the redis.conf configuration file in the Redis installation directory.

maxmemory 4096mb

2. Through cmd

//设置Redis最大占用内存大小为100M
127.0.0.1:6379> config set maxmemory 4096mb
//获取设置的Redis能使用的最大内存大小
127.0.0.1:6379> config get maxmemory

Insert image description here

Set memory elimination policy

  • noeviction strategy: When the memory space reaches maxmemory, the data will not be eliminated, and an error will be returned when there is new writing.
  • volatile-ttl policy: For key-value pairs with expiration time set, the key-value pairs will be modified according to the order of expiration time. The earlier they expire, the earlier they will be deleted.
  • Volatile-random strategy: Randomly delete key-value pairs with expiration time set.
  • volatile-lru strategy: Use the LRU algorithm to filter key-value pairs with an expiration time and delete them.
  • volatile-lfu strategy: Use the LFU algorithm to filter key-value pairs with an expiration time and delete them.
  • allkeys-random strategy: Randomly select and delete data among all key-value pairs.
  • allkeys-lru strategy: Use the LRU algorithm to filter among all data and delete data.
  • allkeys-lfu strategy: Use the LFU algorithm to filter and delete data in all data.

When using the three strategies volatile-lru, volatile-random, and volatile-ttl, if no key can be eliminated, the same error as noeviction will be returned
Setting method

127.0.0.1:6379> config set maxmemory-policy allkeys-lru

View policy

127.0.0.1:6379> config get maxmemory-policy

Key expiration mechanism

A key expiration mechanism - set an expiration time for the key. Once the expiration time is exceeded, the key will be deleted and the memory will be recycled.

When setting the key value, the EXPIRE unit is seconds and the PEXPIRE unit is milliseconds
In Redis, you can use the following command to set the expiration time of the key:

  1. EXPIRE key ttl: Set the key to expire after ttl seconds.

For example, to set a key named "username" to expire after 60 seconds, you can execute the following command:

EXPIRE username 60

If the setting is successful, 1 will be returned; if the key does not exist or the setting fails, 0 will be returned.

  1. PEXPIRE key milliseconds: Sets the key to expire after milliseconds.

For example, to set a key named "email" to expire after 1000 milliseconds, you can execute the following command:

PEXPIRE email 1000

If the setting is successful, 1 will be returned; if the key does not exist or the setting fails, 0 will be returned.

  1. EXPIREAT key timestamp: Set the key to expire after a certain timestamp (accurate to seconds).

For example, to set a key named "password" to expire at 0:01 on January 1, 2023, you can execute the following command:

EXPIREAT password 1672752660

If the setting is successful, 1 will be returned; if the key does not exist or the setting fails, 0 will be returned.

  1. PEXPIREAT key millisecondsTimestamp: Set the key to expire after a certain timestamp (accurate to milliseconds).

For example, to set a key named "access_token" to expire after 0:01:10 on January 1, 2023, you can execute the following command:

PEXPIREAT access_token 1672752670000

If the setting is successful, 1 will be returned; if the key does not exist or the setting fails, 0 will be returned.

It should be noted that the expiration time is only allowed to be set when the key already exists. If the key does not exist, using the above command will return 0. In addition, when a key expires, you can use the TTL and PTTL commands to view the remaining survival time of the key. If the key does not exist or the expiration time is not set, -1 or -2 will be returned.
The unit of TTL is seconds, and the unit of PTTL is milliseconds

127.0.0.1:6379> TTL KEY
127.0.0.1:6379> PTTL KEY

Guess you like

Origin blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/134530831