Redis 安装与基础使用 Print

  • 45

Redis 是一款高性能的键值存储系统,常用于缓存、会话管理和消息队列。

安装

sudo apt update
sudo apt install redis-server
sudo systemctl enable redis-server
sudo systemctl start redis-server

验证安装

redis-cli ping
# 返回 PONG

5 种基本数据类型

# String(字符串)
SET name "Alice"
GET name

# Hash(哈希)
HSET user:1 name "Alice" age 30
HGET user:1 name
HGETALL user:1

# List(列表)
LPUSH queue "task1"
RPUSH queue "task2"
LRANGE queue 0 -1

# Set(集合)
SADD tags "linux"
SADD tags "nginx"
SMEMBERS tags

# Sorted Set(有序集合)
ZADD leaderboard 100 "player1"
ZADD leaderboard 200 "player2"
ZRANGE leaderboard 0 -1 WITHSCORES

安全配置

编辑 /etc/redis/redis.conf

requirepass YourStrongPassword
bind 127.0.0.1 ::1
rename-command FLUSHALL ""
rename-command FLUSHDB ""
sudo systemctl restart redis-server

持久化配置

# RDB 快照(默认开启)
save 900 1
save 300 10
save 60 10000

# AOF 日志
appendonly yes
appendfsync everysec

Was this answer helpful?

« Back