redis 常用命令

栏目: redis 发布时间:2024-11-01

使用 docker 搭建 redis 服务

配置 docker-compose.yml

# docker-compose.yml
version: '3'
services:
  redis:
    image: 'daocloud.io/library/redis'
    ports:
      - '6379:6379'
    container_name: 'fe-redis'

创建 redis 容器

docker-compose up -d

进入redis容器

docker exec -it fe-redis bash

在容器中执行:

redis-cli

接着可以执行各种 redis 命令了

keys *

列举出所有的 key

127.0.0.1:6379> keys *
 1) "bull:check conflicts of git branches:11:lock"
 2) "bull:check conflicts of git branches:14"
 3) "bull:my-first-queue:id"
 4) "bull:check conflicts of git branches:9"
 5) "bull:check conflicts of git branches:16"
 6) "bull:check conflicts of git branches:stalled-check"
 7) "bull:check conflicts of git branches:failed"
 8) "bull:check conflicts of git branches:4"
 9) "bull:check conflicts of git branches:wait"
10) "bull:check conflicts of git branches:id"
11) "bull:check conflicts of git branches:1"
12) "bull:my-first-queue:active"
13) "bull:check conflicts of git branches:5"
14) "bull:check conflicts of git branches:6"
15) "bull:check conflicts of git branches:17"
16) "bull:check conflicts of git branches:2"
17) "bull:check conflicts of git branches:7"
18) "bull:check conflicts of git branches:13"
19) "bull:check conflicts of git branches:active"
20) "bull:check conflicts of git branches:15"
21) "bull:check conflicts of git branches:3"
22) "bull:my-first-queue:1"
23) "bull:check conflicts of git branches:11"
24) "bull:my-first-queue:2"
25) "bull:check conflicts of git branches:10"
26) "bull:check conflicts of git branches:8"
27) "bull:check conflicts of git branches:18"
28) "bull:check conflicts of git branches:12"
127.0.0.1:6379>

使用 * 号获取所有配置项:

redis 127.0.0.1:6379> CONFIG GET *

SET

set name lucy

GET

get name

DEL

删除 key

del name

FLUSHALL

清空数据

FLUSHALL

Redis 列表(List)

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)

一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。

  • LPUSH 向列表头部插入一个元素
LPUSH user lucy
LPUSH user lily
  • LRANGE 查看列表
LRANGE user 0 10

结果:

1) "lily"
2) "lucy"
  • RPUSH 向列表尾部插入一个元素
RPUSH user tom
LRANGE user 0 10

结果:

1) "lily"
2) "lucy"
3) "tom"
  • LPOP 移除并获取列表的第一个元素
LPOP user
  • RPOP 移除并获取列表的最后一个元素
RPOP user

Set(集合)

Redis 的 Set 是 string 类型的无序集合。

SADD user lucy
SADD user lily
SMEMBERS user

结果:

1) "lily"
2) "lucy"

本文地址:https://www.tides.cn/p_redis-guide