redis 7
详细安装
步骤请至https://blog.csdn.net/qq_38534887/article/details/131014996
1. 字符串
1.0 用途
1. 点赞, 给
id
为1000
的文章点赞, 数量+ 1
127.0.0.1:6379> set article-count:1000 999 OK 127.0.0.1:6379> incr article-count:1000 (integer) 1000 127.0.0.1:6379> get article-count:1000 "1000"
1.1 设置过期时间
127.0.0.1:6379> set a 12 ex 60 # 通过过 ex 设置过期时间 OK 127.0.0.1:6379> ttl a (integer) 57 127.0.0.1:6379> set a 23 # 如果这样设置之后,就会导致给a设置无限长时间 OK 127.0.0.1:6379> ttl a (integer) -1 127.0.0.1:6379> set a 99 keepttl # 这样的话设置新的值之后不会更改过期时间, 保持>原来的过期时间 OK 127.0.0.1:6379> ttl a (integer) 4 127.0.0.1:6379> ttl a (integer) 1
1.2 设置多值
127.0.0.1:6379> mset a 1 b 2 c 3 OK 127.0.0.1:6379> mget a b c 1) "1" 2) "2" 3) "3"
1.3 范围获取/设置
127.0.0.1:6379> set a 1234567890 OK 127.0.0.1:6379> getrange a 1 2 "23" 127.0.0.1:6379> setrange a 1 p (integer) 10 127.0.0.1:6379> get a "1p34567890" 127.0.0.1:6379> getrange a 0 1 # 根据索引来获取字符串 "1p" 127.0.0.1:6379> substr a 0 1 # 该方法已弃用,不推荐使用, 推荐使用 getrange "1p"
1.4 递增/递减
127.0.0.1:6379> get b "120" 127.0.0.1:6379> incr b (integer) 121 127.0.0.1:6379> incrby b 3 # 3 为 递增间隔 (integer) 124 127.0.0.1:6379> decr b (integer) 123 127.0.0.1:6379> decrby b 3 # 3 为递减间隔 (integer) 120
1.5 获取长度 / 追加
127.0.0.1:6379> get a "1p34567890" 127.0.0.1:6379> strlen a # 获取长度 (integer) 10 127.0.0.1:6379> append a ppp # 末尾追加 ppp (integer) 13 127.0.0.1:6379> get a "1p34567890ppp"
1.6 分布式锁(setnx / set xx)
127.0.0.1:6379> get a "1p34567890ppp" 127.0.0.1:6379> setnx a 22 # a 存在设置不成功 (integer) 0 127.0.0.1:6379> del a (integer) 1 127.0.0.1:6379> setnx a 11111 # a 不存在设置成功 (integer) 1 127.0.0.1:6379> del a (integer) 1 127.0.0.1:6379> set a 333 nx # setnx 也可以合并在一起操作, 不存在设置成功,存在不设置 OK 127.0.0.1:6379> set a 444 nx # 存在继续设置,就设置不了 (nil) 127.0.0.1:6379> get a "11111" 127.0.0.1:6379> set a 222222 xx # a 存在 设置成功, 与 nx 相反 OK 127.0.0.1:6379> get a "222222"
1.7 获取并且设置
127.0.0.1:6379> set a 11111 OK 127.0.0.1:6379> getset a 22222 # 返回原先的值,在重新设置新的值 "11111"
2. list
2.0 用途,订阅公众号
查看
微信订阅号
的信息,可以使用, 比如我订阅了两个
id
为1
和2
的公众号127.0.0.1:6379> lpush gzh:uid 1 2 (integer) 2 127.0.0.1:6379> lrange gzh:uid 0 -1 1) "2" 2) "1"
2.1 添加 (lpush, rpush)-----注意添加顺序
127.0.0.1:6379> lpush list 1 5 2 3 # 从左边开始添加 (integer) 4 127.0.0.1:6379> lrange list 0 -1 1) "3" 2) "2" 3) "5" 4) "1" ---------------------------------------------------(rpush) 127.0.0.1:6379> lpush l1 6 4 2 # 注意添加顺序和 获取顺序, 不一致, 2 最后添加 (integer) 3 127.0.0.1:6379> lrange l1 0 -1 1) "2" 2) "4" 3) "6" 127.0.0.1:6379> rrange rlist 0 -1 (error) ERR unknown command 'rrange', with args beginning with: 'rlist' '0' '-1'
2.2 获取 (lrange , lindex-索引获取 , llen-获取长度)
127.0.0.1:6379> lpush list 1 5 2 3 (integer) 8 127.0.0.1:6379> lrange rlist 0 -1 # 获取list 中元素, 没有rrange 1) "3" 2) "3" 3) "4" 4) "5" 127.0.0.1:6379> rrange rlist 0 -1 # 没有 rrange命令 (error) ERR unknown command 'rrange', with args beginning with: 'rlist' '0' '-1' ---------------------------------------------------(lindex) 通过索引获取值 127.0.0.1:6379> lrange list 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "3" 127.0.0.1:6379> lindex list 1 # 通过索引获取 "5" 127.0.0.1:6379> lindex list 0 "6" ---------------------------------------------------(llen) 通过索引获取值
2.3 删除 (lpop, rpop, lrem -删除 几次这样的值)
127.0.0.1:6379> lrange list 0 -1 1) "3" 2) "2" 3) "5" 4) "1" 5) "3" 6) "2" 7) "5" 8) "1" 127.0.0.1:6379> lpop list 1 # 从左边 删除 1 个元素 1) "3" ---------------------------------------------------(rpop) 127.0.0.1:6379> lrange list 0 -1 1) "3" 2) "2" 3) "5" 4) "1" 127.0.0.1:6379> rpop list 3 # 从右边开始删除 3 个元素 1) "1" 2) "5" 3) "2" ---------------------------------------------------(lrem 删除指定值,几次) 127.0.0.1:6379> lrange list 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "3" 127.0.0.1:6379> lrem list 1 3 # 删除 list 中 1次 等于3的值 (integer) 1 127.0.0.1:6379> lrange list 0 -1 1) "6" 2) "5" 3) "4" 4) "3"
2.4 修改 (ltrim, rpoplpush, lset, linsert)
---------------------------------------------------(ltrim) 127.0.0.1:6379> lrange list 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 127.0.0.1:6379> ltrim list 0 2 # 截取 索引 0 - 2 重新复制给 list OK 127.0.0.1:6379> lrange list 0 -1 1) "6" 2) "5" 3) "4" ---------------------------------------------------(rpoplpush, 弹出第一个键的最右边元素, 放入到第二个键的最左边) 127.0.0.1:6379> lrange l1 0 -1 1) "2" 2) "4" 3) "6" 127.0.0.1:6379> lrange l2 0 -1 1) "1" 2) "3" 3) "5" 127.0.0.1:6379> rpoplpush l1 l2 "6" 127.0.0.1:6379> lrange l1 0 -1 1) "2" 2) "4"> 127.0.0.1:6379> lrange l2 0 -1 1) "6" 2) "1" 3) "3" 4) "5" ---------------------------------------------------(lset, 修改指定位置的值) 127.0.0.1:6379> lrange l1 0 -1 1) "2" 2) "4" 3) "6" 127.0.0.1:6379> lset l1 0 0 # 设置 l1 下标0位置的值 为 0 OK 127.0.0.1:6379> lrange l1 0 -1 1) "0" 2) "4" 3) "6" ---------------------------------------------------(lisnert, 在指定元素之前或者之后添加值) 127.0.0.1:6379> lrange l1 0 -1 1) "1" 2) "3" 3) "5" 127.0.0.1:6379> linsert l1 before 3 9 # 在l1 的3 元素之前添加 9 (integer) 4 127.0.0.1:6379> linsert l1 after 3 9 # 在l1 的3 元素之后添加 9 (integer) 5 127.0.0.1:6379> lrange l1 0 -1 1) "1" 2) "9" 3) "3" 4) "9" 5) "5">
3HASH
3.0 用途 — 购物车
新增商品 → hset shopcar:uid1024 334488 1
新增商品 → hset shopcar:uid1024 334477 1
增加商品数量 → hincrby shopcar:uid1024 334477 1
商品总数 → hlen shopcar:uid1024
全部选择 → hgetall shopcar:uid10243.1 添加(hset, hmset(弃用),hsetnx)
3.1.1 hset
---------------------------------------------------(hmset: 返回添加字段数量) 127.0.0.1:6379> hset user name lvdou age 19 (integer) 2
3.1.2 hmset — 弃用---------------------------------------------------(hmset: 返回 OK, 4.0版本已经弃用,不推荐) 127.0.0.1:6379> hmset user2 name lvdou2 age 20 OK
3.1.3 hsetnx —不存在才设置
127.0.0.1:6379> hsetnx user school xuexiao (integer) 1 127.0.0.1:6379> hsetnx user school xuexiao (integer) 0
3.2 获取(hget, hmget,hgetall,hkeys hvals, hlen)
3.2.1 hget
127.0.0.1:6379> hget user name "lvdou"
3.2.2 hmget
127.0.0.1:6379> hmget user name age 1) "lvdou" 2) "19" 127.0.0.1:6379> hgetall user 1) "name" 2) "lvdou" 3) "age" 4) "19"
3.2.3 hkeys
127.0.0.1:6379> hkeys user 1) "name" 2) "age"
3.2.4 hvals
127.0.0.1:6379> hvals user 1) "lvdou" 2) "19"
3.2.5 hgetall
127.0.0.1:6379> hgetall user 1) "name" 2) "lvdou" 3) "age" 4) "20" 5) "address" 6) "jiangsu" 127.0.0.1:6379> hlen user (integer) 3
3.3 删除(hdel)
---------------------------------------------------(hdel: 删除指定键) 127.0.0.1:6379> hgetall user 1) "name" 2) "lvdou" 3) "age" 4) "19" 127.0.0.1:6379> hdel user age (integer) 1 127.0.0.1:6379> hgetall user 1) "name" 2) "lvdou"
3.5 修改(hincrby , hincrbyfloat)
127.0.0.1:6379> hget user age "20" 127.0.0.1:6379> hincrby user age 1 (integer) 21 127.0.0.1:6379> hincrbyfloat user age 0.5 "21.5"
4 SET
4.0 用途 —
4.0.1 抽奖
描述 命令 1. 用户id, 立即参与按钮 sadd key 用户ID 2. 显示多少人参与 scard key 3. 抽奖(从 set 中随机弹出中奖者) srandmember key 2 (随机抽取 2人, 可重复, 不删除) 3. 抽奖(从 set 中随机弹出中奖者) spop key 2 (随机抽取 2人, 不可重复, 删除) 4.0.2 朋友圈点赞
描述 命令 1 新增点赞 smembers s1 2 取消点赞 srem news:id uid1 3 展现所有点赞过的用户 smembers news:id 4 点赞用户数统计,就是常见的点赞红色数字 scard news:id 5 判断某个朋友是否对楼主点赞过 sismember news:id uid 4.0.3 可能认识的人
4.1 添加 (sadd)
4.1.1 sadd
127.0.0.1:6379> sadd s1 a b b c d (integer) 4 127.0.0.1:6379> del s2 (integer) 1 127.0.0.1:6379> sadd s2 3 2 5 3 1 (integer) 4
4.2 查询 (smembers, sismember, scard, srandmember)
4.2.1 smembers — 查询所有集合元素
127.0.0.1:6379> smembers s1 1) "d" 2) "c" 3) "b" 4) "a"
4.2.2 sismember — 判断是是否在集合中
127.0.0.1:6379> sadd s1 a b b c d (integer) 4 127.0.0.1:6379> del s2 (integer) 1 127.0.0.1:6379> sadd s2 3 2 5 3 1 (integer) 4
4.2.3 sacrd — 查询集合中 元素个数
127.0.0.1:6379> sadd s1 v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> scard s1 (integer) 4
4.2.4 srandmember — 随机获取
指定数量
的值
,不删除
127.0.0.1:6379> sadd s1 v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> scard s1 (integer) 4
4.3 修改
4.4 删除(srem, spop, smove)
4.4.1 srem 删除指定元素
127.0.0.1:6379> srem s1 a (integer) 1 127.0.0.1:6379> smembers s1 1) "d" 2) "c" 3) "b"
4.4.2 spop 随机弹出数量的值
127.0.0.1:6379> smembers s1 1) "v2" 2) "v1" 3) "v4" 4) "v3" 127.0.0.1:6379> spop s1 2 1) "v3" 2) "v1" 127.0.0.1:6379> smembers s1 1) "v2" 2) "v4"
4.4.3 smove 移动元素到另一个集合中
127.0.0.1:6379> smembers s1 1) "v2" 2) "v4" 127.0.0.1:6379> smembers s2 1) "1" 2) "2" 3) "3" 4) "5" 127.0.0.1:6379> smove s1 s2 v2 (integer) 1 127.0.0.1:6379> smembers s2 1) "3" 2) "1" 3) "5" 4) "2" 5) "v2"
4.5 集合运算 (sdiff, sunion,sinter, sintercard)
127.0.0.1:6379> sadd s1 1 2 3 4 5 6 (integer) 6 127.0.0.1:6379> sadd s2 2 4 6 (integer) 3
4.5.1 sdiff — 差集
127.0.0.1:6379> sdiff s1 s2 1) "1" 2) "3" 3) "5"
4.5.2 sunion — 并集
127.0.0.1:6379> sunion s1 s2 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6"
4.5.3 sinter — 交集
127.0.0.1:6379> sinter s1 s2 1) "2" 2) "4" 3) "6"
4.5.4 sintercard --- Redis 7 新命令
sintercard
key的数量
集合key1
集合key2
limit 数量
127.0.0.1:6379> sadd s1 1 3 5 8 (integer) 4 127.0.0.1:6379> sadd s2 2 4 6 8 (integer) 4 127.0.0.1:6379> sadd s3 1 2 4 8 (integer) 4 127.0.0.1:6379> sinter s1 s2 s3 1) "8" 127.0.0.1:6379> sintercard 3 s1 s2 s3 # 不是返回集体交集元素, 而是个数 (integer) 1>
5 ZSET
5.0 用途 商品销售排行榜
描述 命令 1. 购买商品, 购买 1 个gid1
,2个gid2
zadd goods:ph .1 gid1 2 gid2 2. 继续购买, 2个gid1
zincrby goods:ph 2 gid1 3. 根据销量排行 zrange goods:ph 0 -1 withscores 5.0.2 元数据
127.0.0.1:6379> zadd z1 3 a 2 b 7 c (integer) 3 127.0.0.1:6379> zrange z1 0 -1 withscores 1) "b" 2) "2" 3) "a" 4) "3" 5) "c" 6) "7"
5.1 添加 (
zadd
)5.1.1
zadd
127.0.0.1:6379> zadd z1 nx 3 a 2 b 7 c (integer) 3
5.2 获取 (
zrange, zrevrange, zrangebyscore,zscore,zcard ,zcount
)5.2.1
zrange
127.0.0.1:6379> zrange z1 0 -1 1) "b" 2) "a" 3) "c"> 127.0.0.1:6379> zrange z1 0 -1 withscores 1) "b" 2) "2" 3) "a" 4) "3" 5) "c" 6) "7" 127.0.0.1:6379> zrange z1 0 -1 rev # 反序获取 1) "c" 2) "a" 3) "b"
5.2.2
zrevrange
– 反序排列127.0.0.1:6379> zrevrange z1 0 -1 withscores # 反序获取 1) "c" 2) "7" 3) "a" 4) "3" 5) "b" 6) "2"
5.2.3
zrangebyscore
– 根据分数范围获取127.0.0.1:6379> zrangebyscore z1 2 6 withscores # 返回 2 - 6之间的元素 1) "b" 2) "2" 3) "a" 4) "3"
5.2.4
zscore
– 返会元素分数127.0.0.1:6379> zscore z1 a "3"
5.2.5
zcount
– 返会 范围内元素个数127.0.0.1:6379> zrange z1 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "7" 127.0.0.1:6379> zcount z1 2 6 (integer) 1
5.2.6
zcard
– 获取元素个数127.0.0.1:6379> zcard z1 (integer) 3
5.2.7
zrank
– 获取指定元素的下标127.0.0.1:6379> zrange z1 0 -1 withscores # a的下标 为0 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "7" 127.0.0.1:6379> zrank z1 a (integer) 0>
5.3 修改 (zincrby)
5.3.1
zincrby
— 增加数量127.0.0.1:6379> zincrby z1 1 a # 如果a 不存在, 则从 0 开始递增 1 "1"
5.4 删除 (zrem, zmpop)
5.4.1
zrem
— 删除指定的值127.0.0.1:6379> zrem z1 a (integer) 1 127.0.0.1:6379> zrange z1 0 -1 1) "b" 2) "c"
5.4.2
zmpop
— 删除指定数量元素, 并且指定 从大到小还是从小到大删除127.0.0.1:6379> zadd z2 1 a 2 b 3 c 4 d 5 f 6 e (integer) 6 127.0.0.1:6379> zmpop 1个集合 集合名称 要弹出的是从最大的还是最小的开始 count 弹出的数量 127.0.0.1:6379> zmpop 1 z2 min count 2 1) "z2" 2) 1) 1) "a" 2) "1" 2) 1) "b" 2) "2" 127.0.0.1:6379> zmpop 1 z2 max count 2 1) "z2" 2) 1) 1) "e" 2) "6" 2) 1) "f" 2) "5">
5 GEO
5.0 用途
5.0 用途
5.1 增加