String字符串类型
- 字符串基本操作
127.0.0.1:6379> set name yorick # 设置值
OK
127.0.0.1:6379> get name # 获取值
"yorick"
127.0.0.1:6379> keys * # 获取全部key
1) "name"
127.0.0.1:6379> exists name # 判断name是否存在
(integer) 1
127.0.0.1:6379> append name "_jun" # 追加字符串,若当前key不存在则相当于set key
(integer) 10
127.0.0.1:6379> get name
"yorick_jun"
127.0.0.1:6379> strlen name # 获取字符串长度
(integer) 10
127.0.0.1:6379> append name2 "tom"
(integer) 3
127.0.0.1:6379> get name2
"tom"
- 字符串自增自减操作
127.0.0.1:6379> set index 0 # 设置初始值
OK
127.0.0.1:6379> get index
"0"
127.0.0.1:6379> incr index # 自增1
(integer) 1
127.0.0.1:6379> incr index
(integer) 2
127.0.0.1:6379> get index
"2"
127.0.0.1:6379> decr index # 自减1
(integer) 1
127.0.0.1:6379> decr index
(integer) 0
127.0.0.1:6379> get index
"0"
127.0.0.1:6379> incrby index 3 # 增3
(integer) 3
127.0.0.1:6379> decrby index 2 # 减2
(integer) 1
127.0.0.1:6379> get index
"1"
- 截取和替换字符串
127.0.0.1:6379> set sentence "hello world"
OK
127.0.0.1:6379> get sentence
"hello world"
127.0.0.1:6379> getrange sentence 0 4 # 截取字符串 [0,4]
"hello"
127.0.0.1:6379> getrange sentence 0 -1 # 获取全部字符串 等同于 get key
"hello world"
127.0.0.1:6379> setrange sentence 6 XX # 替换从6开始的2两个字符为XX
(integer) 11
127.0.0.1:6379> get sentence
"hello XXrld"
- 设置带过期时间的字符串和不存在设置
127.0.0.1:6379> setex name 10 "yorick" # 设置带过期时间的字符串,表示name 10秒后过期
OK
127.0.0.1:6379> ttl name
(integer) 7
127.0.0.1:6379> get name
"yorick"
127.0.0.1:6379> get name # 已过期
(nil)
127.0.0.1:6379> setnx nosql "redis" # 如果nosql不存在,则创建
(integer) 1
127.0.0.1:6379> get nosql
"redis"
127.0.0.1:6379> setnx nosql "mongodb" # nosql字段存在,则创建失败
(integer) 0
- 批量set和get操作
127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3 # 批量设置多个值
OK
127.0.0.1:6379> mget key1 key2 key3 # 批量获取多个值
1) "value1"
2) "value2"
3) "value3"
127.0.0.1:6379> msetnx key1 newvalue1 key4 newvalue4 # 原子操作,若失败则一起失败
(integer) 0
127.0.0.1:6379> get key4
(nil)
- 使用Redis保存对象的两种方式
127.0.0.1:6379> set user:1 {name:yorick,age:23} # 方式一
OK
127.0.0.1:6379> get user:1
"{name:yorick,age:23}"
127.0.0.1:6379> mset user:1:name tom user:1:age 22 # 方式二
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "tom"
2) "22"
127.0.0.1:6379> keys *
1) "user:1:age"
2) "user:1:name"
3) "user:1"
- getset同步操作
127.0.0.1:6379> getset nosql "redis"
(nil)
127.0.0.1:6379> get nosql
"redis"
127.0.0.1:6379> getset nosql "mongodb"
"redis"
127.0.0.1:6379> get nosql
"mongodb"
List列表类型
- 两侧添加、删除元素、获取list列表中的元素、获取指定下标元素、获取长度、移除指定元素的相关操作
127.0.0.1:6379> lpush list one # 向列表左边插入值
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lrange list 0 -1 # 获取列表中的全部值
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrange list 0 1 # 获取列表指定范围的值
1) "three"
2) "two"
127.0.0.1:6379> rpush list four # 向列表右边插入值
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "four"
127.0.0.1:6379> lpop list # 左边弹出值
"three"
127.0.0.1:6379> rpop list # 右边弹出值
"four"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lindex list 1 # 通过下标获取值
"one"
127.0.0.1:6379> lindex list 0
"two"
127.0.0.1:6379> llen list # 获取列表的长度
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lpush list three
(integer) 4
127.0.0.1:6379> lrem list 1 one # 移除指定个数的精确值
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "two"
- 截取元素操作
127.0.0.1:6379> rpush list one
(integer) 1
127.0.0.1:6379> rpush list two
(integer) 2
127.0.0.1:6379> rpush list three
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> ltrim list 1 2 # 通过下标截取指定长度的元素
OK
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "three"
- 移除列表中最后一个元素并加入到新列表的操作
127.0.0.1:6379> rpush list one
(integer) 1
127.0.0.1:6379> rpush list two
(integer) 2
127.0.0.1:6379> rpush list three
(integer) 3
127.0.0.1:6379> rpoplpush list newlist # 移除列表中最后一个元素并加入到新列表
"three"
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "two"
127.0.0.1:6379> lrange newlist 0 -1
1) "three"
- 判断列表是否存在、向列表的指定下标处替换为另一个值
127.0.0.1:6379> exists list # 查看列表是否存在
(integer) 0
127.0.0.1:6379> lpush list yorick
(integer) 1
127.0.0.1:6379> exists list
(integer) 1
127.0.0.1:6379> lset list 0 jun # 向索引为0处替换值
OK
127.0.0.1:6379> lset list 1 jun # 由于索引1处没有值,所以替换失败
(error) ERR index out of range
127.0.0.1:6379> lrange list 0 -1
1) "jun"
- 插入值的操作
127.0.0.1:6379> rpush list hello
(integer) 1
127.0.0.1:6379> rpush list jun
(integer) 2
127.0.0.1:6379> linsert list before jun yorick # 向jun的前面插入元素
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "yorick"
3) "jun"
127.0.0.1:6379> linsert list after hello world # 向hello的后面插入元素
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "world"
3) "yorick"
4) "jun"
Set集合类型
- 添加、查看、判断是否包含元素、查看集合元素个数、移除指定元素的相关操作
127.0.0.1:6379> sadd set hello # 向集合中添加元素
(integer) 1
127.0.0.1:6379> sadd set yorick
(integer) 1
127.0.0.1:6379> smembers set # 查看集合中的元素
1) "yorick"
2) "hello"
127.0.0.1:6379> sismember set hello # 判断集合中是否包含指定元素
(integer) 1
127.0.0.1:6379> sismember set world
(integer) 0
127.0.0.1:6379> scard set # 查看集合元素个数
(integer) 2
127.0.0.1:6379> sadd set hello # 无法添加相同的元素
(integer) 0
127.0.0.1:6379> sadd set world
(integer) 1
127.0.0.1:6379> scard set
(integer) 3
127.0.0.1:6379> srem set hello # 移除指定元素
(integer) 1
127.0.0.1:6379> scard set
(integer) 2
127.0.0.1:6379> smembers set
1) "world"
2) "yorick"
- 抽选元素、随机移除元素的操作
127.0.0.1:6379> sadd set one
(integer) 1
127.0.0.1:6379> sadd set two
(integer) 1
127.0.0.1:6379> sadd set three
(integer) 1
127.0.0.1:6379> sadd set four
(integer) 1
127.0.0.1:6379> smembers set
1) "one"
2) "two"
3) "four"
4) "three"
127.0.0.1:6379> srandmember set # 随机抽选一个元素
"two"
127.0.0.1:6379> srandmember set
"two"
127.0.0.1:6379> srandmember set
"one"
127.0.0.1:6379> srandmember set 2 # 随机抽选指定个数的元素
1) "one"
2) "two"
127.0.0.1:6379> spop set # 随机移除元素
"one"
127.0.0.1:6379> spop set
"two"
127.0.0.1:6379> spop set
"three"
127.0.0.1:6379> smembers set
1) "four"
- 移动指定元素的操作
127.0.0.1:6379> sadd set1 hello
(integer) 1
127.0.0.1:6379> sadd set1 world
(integer) 1
127.0.0.1:6379> sadd set2 yorick
(integer) 1
127.0.0.1:6379> smove set1 set2 world # 将set1中的world元素移动到set2中
(integer) 1
127.0.0.1:6379> smembers set1
1) "hello"
127.0.0.1:6379> smembers set2
1) "world"
2) "yorick"
- 数学集合的操作
127.0.0.1:6379> sadd set1 a
(integer) 1
127.0.0.1:6379> sadd set1 b
(integer) 1
127.0.0.1:6379> sadd set1 c
(integer) 1
127.0.0.1:6379> sadd set2 c
(integer) 1
127.0.0.1:6379> sadd set2 d
(integer) 1
127.0.0.1:6379> sadd set2 e
(integer) 1
127.0.0.1:6379> sdiff set1 set2 # 差集
1) "a"
2) "b"
127.0.0.1:6379> sinter set1 set2 # 交集
1) "c"
127.0.0.1:6379> sunion set1 set2 # 并集
1) "c"
2) "b"
3) "e"
4) "a"
5) "d"
Hash哈希类型
- 添加、获取、删除、获取长度、的操作
127.0.0.1:6379> hset myhash name yorick # set一个具体的k-v对
(integer) 1
127.0.0.1:6379> hget myhash name # 获取一个字段值
"yorick"
127.0.0.1:6379> hmset myhash name tom age 23 # set多个k-v对
OK
127.0.0.1:6379> hmget myhash name age # 获取多个字段值
1) "tom"
2) "23"
127.0.0.1:6379> hgetall myhash # 获取全部字段值
1) "name"
2) "tom"
3) "age"
4) "23"
127.0.0.1:6379> hdel myhash age # 删除指定字段
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "name"
2) "tom"
127.0.0.1:6379> hlen myhash # 获取hash表字段数量
(integer) 1
127.0.0.1:6379> hexists myhash name # 判断hash表是否有指定字段
(integer) 1
127.0.0.1:6379> hexists myhash age
(integer) 0
- 只获取键或值、自增、若没有则添加的操作
127.0.0.1:6379> hmset myhash name yorick age 23
OK
127.0.0.1:6379> hkeys myhash # 获取全部键
1) "name"
2) "age"
127.0.0.1:6379> hvals myhash # 获取全部值
1) "yorick"
2) "23"
127.0.0.1:6379> hincrby myhash age 2 # 自增指定值
(integer) 25
127.0.0.1:6379> hincrby myhash age -2
(integer) 23
127.0.0.1:6379> hsetnx myhash address beijing # 若没有则添加
(integer) 1
127.0.0.1:6379> hsetnx myhash address tianjing
(integer) 0
127.0.0.1:6379> hkeys myhash
1) "name"
2) "age"
3) "address"
Zset有序集合类型
- 添加、查看的操作
127.0.0.1:6379> zadd myset 1 one # 添加1个值
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three # 添加多个值
(integer) 2
127.0.0.1:6379> zrange myset 0 -1 # 查看元素
1) "one"
2) "two"
3) "three"
- 排序、删除、获取元素个数、指定区间元素个数的操作
127.0.0.1:6379> zadd myset 1000 tom
(integer) 1
127.0.0.1:6379> zadd myset 500 jerry
(integer) 1
127.0.0.1:6379> zadd myset 1500 yorick
(integer) 1
127.0.0.1:6379> zrangebyscore myset -inf +inf # 升序排序显示所有元素
1) "jerry"
2) "tom"
3) "yorick"
127.0.0.1:6379> zrangebyscore myset -inf +inf withscores # 升序排序,并显示scores
1) "jerry"
2) "500"
3) "tom"
4) "1000"
5) "yorick"
6) "1500"
127.0.0.1:6379> zrangebyscore myset -inf 1000 withscores # 升序排序,显示不超过1000
1) "jerry"
2) "500"
3) "tom"
4) "1000"
127.0.0.1:6379> zrange myset 0 -1
1) "jerry"
2) "tom"
3) "yorick"
127.0.0.1:6379> zrem myset tom # 删除tom元素
(integer) 1
127.0.0.1:6379> zrange myset 0 -1
1) "jerry"
2) "yorick"
127.0.0.1:6379> zcard myset # 获取集合元素个数
(integer) 2
127.0.0.1:6379> zcount myset 500 1000 # 获取score在[500,1000]的元素
(integer) 1
127.0.0.1:6379> zcount myset 500 1500
(integer) 2
参考地址:https://www.bilibili.com/video/BV1S54y1R7SB
网友评论