redis命令速查

首页 / 其他 / 正文

Redis基础命令

基本命令

1. select 切换数据库

127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> 

2. dbsize 查看DB大小

127.0.0.1:6379[2]> dbsize
(integer) 0
127.0.0.1:6379[2]> 

3. Keys * 查看当前库中所有的key

127.0.0.1:6379[2]> set a 123
OK
127.0.0.1:6379[2]> set b 222
OK
127.0.0.1:6379[2]> keys *
1) "a"
2) "b"
127.0.0.1:6379[2]> 

4. flushdb 清空当前库

127.0.0.1:6379[2]> flushdb
OK
127.0.0.1:6379[2]> keys *
(empty array)
127.0.0.1:6379[2]> 

5. flushall 清空所有库

127.0.0.1:6379[2]> flushall
OK
127.0.0.1:6379[2]> 

6. exists 判断key是否存在

127.0.0.1:6379[2]> set a 123
OK
127.0.0.1:6379[2]> set b 222
OK
127.0.0.1:6379[2]> exists a
(integer) 1                # 存在返回1
127.0.0.1:6379[2]> exists c
(integer) 0                # 不存在返回0
127.0.0.1:6379[2]> 

7. expire 设置key的过期时间

127.0.0.1:6379[2]> expire a 30   # 单位是秒
(integer) 1

8. ttl 查看key的剩余过期时间

127.0.0.1:6379[2]> ttl a
(integer) 28
127.0.0.1:6379[2]> ttl a
(integer) 26
127.0.0.1:6379[2]> ttl b
(integer) -1    # -1 没有过期时间,不会过期
127.0.0.1:6379[2]> 

9. type查看key的类型

127.0.0.1:6379[2]> type b
string
127.0.0.1:6379[2]> 

10. info replication 查看当前库主从信息

127.0.0.1:6379> info replication
# Replication
role:master       # 当前角色
connected_slaves:0   # 没有从机
master_failover_state:no-failover
master_replid:dd97240949f1c206a54a3d5513eda5ea59b17703
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> 

11. SLAVEOF 选择主机

SLAVEOF ip地址 端口号

字符串相关命令

1. set 设置值

127.0.0.1:6379> set a 123
OK
127.0.0.1:6379> 

2. get 获取值

127.0.0.1:6379> get a
"123"
127.0.0.1:6379> 

3. append 追加字符串

# 当key不存的时候会自动创建
127.0.0.1:6379> append a abc
(integer) 6
127.0.0.1:6379> append c 222
(integer) 3
127.0.0.1:6379> get a
"123abc"
127.0.0.1:6379> get c
"222"
127.0.0.1:6379> 

4. strlen 获取字符串的长度

127.0.0.1:6379> strlen a
(integer) 6
127.0.0.1:6379> 

5. incr 自增1

127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views 
"0"
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> get views
"1"
127.0.0.1:6379> incr evi # 不存在的key会自增成为1
(integer) 1
127.0.0.1:6379> get evi
"1"
127.0.0.1:6379> 

6. decr 自减1

127.0.0.1:6379> get views
"1"
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> decr views
(integer) -1
127.0.0.1:6379> get views
"-1"
127.0.0.1:6379> 

7.incrby 自增指定步长

127.0.0.1:6379> incrby views 5
(integer) 4
127.0.0.1:6379> get views
"4"
127.0.0.1:6379> 

8. decrby 自减指定步长

127.0.0.1:6379> get views
"4"
127.0.0.1:6379> decrby views 4
(integer) 0
127.0.0.1:6379> 

9. getrange 获取字符串范围

127.0.0.1:6379> set a zheshiyigezifuc
OK
127.0.0.1:6379> get a
"zheshiyigezifuc"
127.0.0.1:6379> getrange a 0 3
"zhes"
127.0.0.1:6379> 

10.setrange 指定位置开始替换字符串

127.0.0.1:6379> get a
"thishiyigezifuc"  # 原字符串 zheshiyigezifuc
127.0.0.1:6379> 

11. setex 设置过期时间

127.0.0.1:6379> setex b 30 "this is a book" # 单位是秒
OK
127.0.0.1:6379> ttl b
(integer) 27
127.0.0.1:6379> 

12. setnx 不存在在设置

127.0.0.1:6379> setnx a 123 
(integer) 0    # a是存在的,所以设置失败
127.0.0.1:6379> setnx c 222
(integer) 1    # c 不存在,设置成功
127.0.0.1:6379> 

13. mset同时设置多个值

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> mset a 1 b 2 c 3
OK
127.0.0.1:6379> keys *
1) "a"
2) "c"
3) "b"
127.0.0.1:6379> 

14. mget 同时获取多个值

127.0.0.1:6379> mget a b c 
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> 

15. msetnx 如果有一个值存在,就会设置不成功

127.0.0.1:6379> keys *
1) "a"
2) "c"
3) "b"
127.0.0.1:6379> msetnx d 5 e 6
(integer) 1
127.0.0.1:6379> msetnx e 6 f 7
(integer) 0
127.0.0.1:6379> 

15. getset 先获取在设置

127.0.0.1:6379> get a
"1"
127.0.0.1:6379> getset a 1122 # 先获取在设置
"1"
127.0.0.1:6379> get a
"1122"
127.0.0.1:6379> getset abc 123 # 不存在的返回nil
(nil)
127.0.0.1:6379> get abc
"123"
127.0.0.1:6379> 

list列表相关命令

1. lpush 将一个或多个值插入到列表左边

127.0.0.1:6379> lpush list 1 # 插入一个值
(integer) 1
127.0.0.1:6379> lpush list 2 3 # 插入多个值
(integer) 3
127.0.0.1:6379> 

2. lrange 获取list中的值

127.0.0.1:6379> lrange list 0 1
1) "3"
2) "2"
127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "2"
3) "1"

3. rpush 将一个值或多个值插入到列表右边

127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> rpush list 0
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "2"
3) "1"
4) "0"
127.0.0.1:6379> 

4. lpop 移除列表左边第一个或指定个数元素

127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "2"
3) "1"
4) "0"
127.0.0.1:6379> lpop list # 移除左边第一个
"3"
127.0.0.1:6379> lrange list 0 -1
1) "2"
2) "1"
3) "0"
127.0.0.1:6379> lpop list 2 # 移除左边两个
1) "2"
2) "1"
127.0.0.1:6379> 

5. rpop 移除列表右边第一个或指定个数元素

127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
127.0.0.1:6379> rpop list
"1"
127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
127.0.0.1:6379> rpop list 3
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
127.0.0.1:6379> 

6. lindex通过下标获取列表中的值

127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
127.0.0.1:6379> lindex list 1
"5"
127.0.0.1:6379> 

7. llen获取列表长度

127.0.0.1:6379> llen list
(integer) 2
127.0.0.1:6379> 

8. lrem移除集合中指定个数的元素

127.0.0.1:6379> lpush list 1 2 3 1 2
(integer) 5
127.0.0.1:6379> lrange list 0 -1
1) "2"
2) "1"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lrem list 1 1 # 从左边开始移除一个1
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "2"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> lrem list 2 2 # 从左边开始移除2个2
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "1"
127.0.0.1:6379> 

9. ltrim 通过下标留下指定长度的元素

127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "1"
127.0.0.1:6379> ltrim 1 2
127.0.0.1:6379> ltrim list 1 2 # 从index第1个开始,留下两个,但不包括index为1的值
OK
127.0.0.1:6379> lrange list 0 -1
1) "5"
2) "4"
127.0.0.1:6379> 

10. rpoplpush将列表最后一个元素移动到新列表中

127.0.0.1:6379> lpush list 1 2 3 4 5 
(integer) 5
127.0.0.1:6379> rpoplpush list list1
"1"
127.0.0.1:6379> lrange list1 0 -1
1) "1"
127.0.0.1:6379> 

11. lset设置列表中指定index的值

127.0.0.1:6379> lpush list 1 2 3 4 
(integer) 4
127.0.0.1:6379> lset list 0 0
OK
127.0.0.1:6379> lrange list 0 -1
1) "0"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> 

12. linsert将具体的值插入到某个元素的前面或后面

127.0.0.1:6379> lrange list 0 -1
1) "0"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> linsert list before 1 h # 将h插在1前边
(integer) 5
127.0.0.1:6379> lrange list 0 -1
1) "0"
2) "3"
3) "2"
4) "h"
5) "1"
127.0.0.1:6379> linsert list after 3 hel # 将hel插在3后边
(integer) 6
127.0.0.1:6379> lrange list 0 -1
1) "0"
2) "3"
3) "hel"
4) "2"
5) "h"
6) "1"
127.0.0.1:6379> 

set集合相关命令

1. sadd 在集合中添加元素

127.0.0.1:6379> sadd set 1
(integer) 1
127.0.0.1:6379> sadd set 2 3 # 一次添加多个元素
(integer) 2
127.0.0.1:6379> 

2. smembers 查看集合中所有的元素

127.0.0.1:6379> smembers set
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> 

3. sismember 判断某一个值是否是这个集合的

127.0.0.1:6379> sismember set 1
(integer) 1
127.0.0.1:6379> sismember set 6 
(integer) 0    # 不属于该集合
127.0.0.1:6379> 

4. scard 获取集合中的元素个数

127.0.0.1:6379> scard set
(integer) 3
127.0.0.1:6379> 

5. srem 移除集合中的某个元素

127.0.0.1:6379> srem set 1
(integer) 1
127.0.0.1:6379> 

6. srandmember 随机获取元素

127.0.0.1:6379> srandmember set # 随机获取一个元素
"3"
127.0.0.1:6379> srandmember set 2 # 随机获取两个元素
1) "2"
2) "3"
127.0.0.1:6379> 

7. spop 随机删除一个元素

127.0.0.1:6379> smembers set
1) "2"
2) "3"
127.0.0.1:6379> spop set 
"2"
127.0.0.1:6379> smembers set
1) "3"
127.0.0.1:6379> 

8. smove 将集合中的指定元素移动到另外一个集合中

127.0.0.1:6379> sadd set 1 2 3 4 5 
(integer) 5
127.0.0.1:6379> smove set set1 1
(integer) 1
127.0.0.1:6379> smembers set1
1) "1"
127.0.0.1:6379> 

9. sdiff差集

127.0.0.1:6379> sadd set 1 2 3 4 5
(integer) 5
127.0.0.1:6379> sadd set1 4 3 6 7 8
(integer) 5
127.0.0.1:6379> sdiff set set1
1) "1"
2) "2"
3) "5"
127.0.0.1:6379> 

10. sinter 交集

127.0.0.1:6379> sinter set set1
1) "3"
2) "4"
127.0.0.1:6379> 

11. sunion并集

127.0.0.1:6379> sunion set set1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
127.0.0.1:6379> 

hash哈希相关命令

1. hset设置一个key-value

127.0.0.1:6379> hset hash key1 value1
(integer) 1
127.0.0.1:6379> 

2. hget获取一个hash字段值

127.0.0.1:6379> hget hash key1
"value1"
127.0.0.1:6379> 

3. hmset 一次设置多个

127.0.0.1:6379> hmset hash key2 value2 key3 value3
OK
127.0.0.1:6379> 

4. hmget 一次获取多个

127.0.0.1:6379> hmget hash key1 key2
1) "value1"
2) "value2"
127.0.0.1:6379> 

5. hgetall 获取全部的

127.0.0.1:6379> hgetall hash
1) "key1"
2) "value1"
3) "key2"
4) "value2"
5) "key3"
6) "value3"
127.0.0.1:6379> 

6. hdel 删除指定的

127.0.0.1:6379> hdel hash key1
(integer) 1
127.0.0.1:6379> 

7. hlen 获取hash数量

127.0.0.1:6379> hlen hash
(integer) 2
127.0.0.1:6379> 

8. hkeys获取所有hash中的键

127.0.0.1:6379> hkeys hash
1) "key2"
2) "key3"
127.0.0.1:6379> 

9. hvals获取所有的值

127.0.0.1:6379> hvals hash
1) "value2"
2) "value3"
127.0.0.1:6379> 

10. hincrby自增

127.0.0.1:6379> hset hash key 1
(integer) 1
127.0.0.1:6379> hincrby hash key 1
(integer) 2
127.0.0.1:6379> hincrby hash key -2
(integer) 0
127.0.0.1:6379> 

11. hsetnx 如果不存在就可以创建

127.0.0.1:6379> hsetnx hash key 2
(integer) 0
127.0.0.1:6379> hsetnx hash key2 1
(integer) 1
127.0.0.1:6379> 

zset有序集合相关命令

1.zadd 添加一个值

127.0.0.1:6379> zadd set 1 a
(integer) 1
127.0.0.1:6379> zadd set 2 b 3 c # 添加多个值
(integer) 2
127.0.0.1:6379> 

2.zrange 获取集合中的值

127.0.0.1:6379> zrange set 0 -1
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> 

3. zrangebyscore从小到达排序

127.0.0.1:6379> zadd salary 2500 zhangsan 5000 lisi 3000 wangwu
(integer) 3
127.0.0.1:6379> zrangebyscore salary -inf +inf
1) "zhangsan"
2) "wangwu"
3) "lisi"
127.0.0.1:6379> 

4. zrevrange 从大到小排序

127.0.0.1:6379> zrevrange salary 0 -1
1) "lisi"
2) "wangwu"
3) "zhangsan"
127.0.0.1:6379> 

5.zrem移除指定元素

127.0.0.1:6379> zrem salary lisi
(integer) 1
127.0.0.1:6379> 

6.zcard 获取有序集合个数

127.0.0.1:6379> zcard salary
(integer) 2
127.0.0.1:6379> 

Geospatial 地理位置相关命令

1.geoadd 添加一个经纬度

有效的经度从-180度到180度。

有效的纬度从-85.05112878度到85.05112878度。

127.0.0.1:6379> geoadd city 116.405285 39.904989 beijing
(integer) 1
127.0.0.1:6379> geoadd city 116.36003 39.9305 xicheng 116.2229 39.90564 shijingshan # 一次添加多个
(integer) 2
127.0.0.1:6379> 

2. geopos 获取坐标值

127.0.0.1:6379> geopos city beijing
1) 1) "116.40528291463851929"
   2) "39.9049884229125027"
127.0.0.1:6379> geopos city beijing shijingshan
1) 1) "116.40528291463851929"
   2) "39.9049884229125027"
2) 1) "116.22289806604385376"
   2) "39.90563984625044469"
127.0.0.1:6379> 

3. geodist两个坐标之间的距离

  • m 米
  • km 千米
  • mi 英里
  • ft英尺
127.0.0.1:6379> geodist city beijing shijingshan m
"15561.6651"
127.0.0.1:6379> geodist city beijing shijingshan km
"15.5617"
127.0.0.1:6379> 

4. georadius 获取附近位置通过半径查找

127.0.0.1:6379> georadius city 116.40528291463851929 39.9049884229125027 10 km # 10km为半径 116.40528291463851929经度 39.9049884229125027 纬度
1) "beijing"
2) "xicheng"
127.0.0.1:6379> georadius city 116.40528291463851929 39.9049884229125027 1 km
1) "beijing"
127.0.0.1:6379> georadius city 116.40528291463851929 39.9049884229125027 10 km withdist # 显示距离
1) 1) "beijing"
   2) "0.0000"
2) 1) "xicheng"
   2) "4.7912" # 距离中心多远
127.0.0.1:6379> georadius city 116.40528291463851929 39.9049884229125027 10 km withdist withcoord # 在加上显示经纬度
1) 1) "beijing"
   2) "0.0000"
   3) 1) "116.40528291463851929"
      2) "39.9049884229125027"
2) 1) "xicheng"
   2) "4.7912"
   3) 1) "116.36002868413925171"
      2) "39.93050039138125129"
127.0.0.1:6379> georadius city 116.40528291463851929 39.9049884229125027 10 km withdist withcoord count 1 # 在加上获取个数
1) 1) "beijing"
   2) "0.0000"
   3) 1) "116.40528291463851929"
      2) "39.9049884229125027"
127.0.0.1:6379> 

5. georadiusbymember 根据地理名称获取周围的信息

127.0.0.1:6379> georadiusbymember city beijing 10 km
1) "beijing"
2) "xicheng"
127.0.0.1:6379> 

6. geohash 返回一个或多个元素的gohash标识

127.0.0.1:6379> geohash city beijing
1) "wx4g0b7xrt0"
127.0.0.1:6379> geohash city beijing shijingshan
1) "wx4g0b7xrt0"
2) "wx4eh8wgjy0"
127.0.0.1:6379> 

hyperloglog 相关命令

1. pfadd 创建一组元素

127.0.0.1:6379> pfadd key a b c d e 
(integer) 1
127.0.0.1:6379> 

2. pfcount 统计基数数量

127.0.0.1:6379> pfcount key 
(integer) 5
127.0.0.1:6379> 

3. pfmerge 合并两组

127.0.0.1:6379> pfcount key
(integer) 9
127.0.0.1:6379> pfadd key1 a b c h l m n
(integer) 1
127.0.0.1:6379> pfmerge key3 key key1
OK
127.0.0.1:6379> pfcount key3
(integer) 13
127.0.0.1:6379> 

bitmap 相关命令

1. setbit 设置

127.0.0.1:6379> setbit zhangsan 1 0
(integer) 0
127.0.0.1:6379> setbit zhangsan 2 0
(integer) 0
127.0.0.1:6379> setbit zhangsan 3 1
(integer) 0
127.0.0.1:6379> 

2. getbit 获取

127.0.0.1:6379> getbit zhangsan 1
(integer) 0
127.0.0.1:6379> getbit zhangsan 3
(integer) 1
127.0.0.1:6379> 

事务相关命令

1. multi 开启事务

127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> set key 1
QUEUED
127.0.0.1:6379(TX)> set key1 2
QUEUED
127.0.0.1:6379(TX)> 

2. exec 执行事务

127.0.0.1:6379(TX)> exec
1) OK
2) OK
127.0.0.1:6379> get key 
"1"
127.0.0.1:6379> 

3. discard 放弃事务

127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> set a 123
QUEUED
127.0.0.1:6379(TX)> discard
OK
127.0.0.1:6379> get a 
(nil)
127.0.0.1:6379> 
打赏
评论区
头像
文章目录