关于redis:免费领取红包封面Redis-系列redis-学习四set-集合hash-哈希zset-有序集合初步认知

文末收费支付红包封面哦,总共 2000 个 ,先到先得

set 汇合

set 汇合外面的数据是不能重读的

  • SADD key member [member …]

向 set 汇合中增加元素

  • SMEMBERS key

查看汇合中的所有元素

  • SISMEMBER key member

查看某一个数据是否在汇合中

  • SCARD key

查看汇合数据的个数,也就是汇合的长度

  • SREM key member [member …]

移除汇合中指定的元素

127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "wolrd" "xiaomotong"
(integer) 2
127.0.0.1:6379> SMEMBERS myset
1) "wolrd"
2) "xiaomotong"
3) "hello"
127.0.0.1:6379> SISMEMBER myset hello
(integer) 1
127.0.0.1:6379> SCARD myset
(integer) 3
127.0.0.1:6379> SREM myset wolrd
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "hello"
  • SRANDMEMBER key [count]

随机获取汇合外面的数据,能够指定个数

127.0.0.1:6379> sadd myset "v1" "v2" "v3" "v4"
(integer) 4
127.0.0.1:6379> SMEMBERS myset
1) "v4"
2) "xiaomotong"
3) "v1"
4) "v2"
5) "hello"
6) "v3"
127.0.0.1:6379> SRANDMEMBER myset
"v2"
127.0.0.1:6379> SRANDMEMBER myset
"xiaomotong"
127.0.0.1:6379> SRANDMEMBER myset 2
1) "v4"
2) "v1"
127.0.0.1:6379>
127.0.0.1:6379> SRANDMEMBER myset 2
1) "xiaomotong"
2) "v1"
  • SPOP key [count]

随机删除汇合中的任意元素

127.0.0.1:6379> SMEMBERS myset
1) "v4"
2) "xiaomotong"
3) "v1"
4) "v2"
5) "hello"
6) "v3"
127.0.0.1:6379> SPOP myset
"v2"
127.0.0.1:6379> SMEMBERS myset
1) "v4"
2) "xiaomotong"
3) "v1"
4) "hello"
5) "v3"
127.0.0.1:6379> SPOP myset
"v4"
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "v1"
3) "hello"
4) "v3"
  • SMOVE source destination member

从指定的汇合中取出一个元素,放到另外一个汇合中

127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "v1"
3) "hello"
4) "v3"
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "v1"
3) "hello"
4) "v3"
127.0.0.1:6379> SMOVE myset newset v1
(integer) 1
127.0.0.1:6379> SMEMBERS newset
1) "v1"
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "hello"
3) "v3"
  • SUNION key [key …]

取并集,对两个汇合取并集

  • SINTER key [key …]

取交加,对两个汇合取交加

127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "hello"
3) "v3"
127.0.0.1:6379> sadd newset v3
(integer) 1
127.0.0.1:6379> sadd newset v6 v9
(integer) 2
127.0.0.1:6379> SUNION myset newset
1) "v3"
2) "hello"
3) "xiaomotong"
4) "v1"
5) "v6"
6) "v9"
127.0.0.1:6379> SINTER myset newset
1) "v3"
127.0.0.1:6379>

set 利用场景能够有,多个用户的独特关注,好友举荐,独特的粉丝,性能的喜好等等

hash 哈希

Map 汇合,key-map,此时 key-value 是一个汇合,实质上和 string 类型没有啥区别,还是一个简略的 key-value 模式

  • HSET key field value [field value …] / HMSET key field value [field value …]

在 hash key 中增加 1 个或者多个 key-value

  • HGET key field

获取 hash 外面的一个 key 对应的 value

  • HMGET key field [field …]

获取 hash 外面的多个 key 对应的 value

  • HGETALL key

获取 hash 外面所有的 键值对

127.0.0.1:6379> hset myhash name xiaomotong age 12 hobby play
(integer) 3
127.0.0.1:6379> hget myhash name
"xiaomotong"
127.0.0.1:6379> hmget myhash name age
1) "xiaomotong"
2) "12"
127.0.0.1:6379> hgetall myhash
1) "name"
2) "xiaomotong"
3) "age"
4) "12"
5) "hobby"
6) "play"
  • HDEL key field [field …]

删除 hash 外面的 key,一个或者多个

127.0.0.1:6379> HSET myhash k1 v1 k2 v2 k3 v3
(integer) 3
127.0.0.1:6379> hgetall
(error) ERR wrong number of arguments for 'hgetall' command
127.0.0.1:6379> hgetall myhash
 1) "age"
 2) "12"
 3) "hobby"
 4) "play"
 5) "k1"
 6) "v1"
 7) "k2"
 8) "v2"
 9) "k3"
10) "v3"
127.0.0.1:6379> HDEL myhash k1 k2
(integer) 2
127.0.0.1:6379> HDEL myhash name
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "age"
2) "12"
3) "hobby"
4) "play"
5) "k3"
6) "v3"
  • HLEN key

获取 hash 的长度,也就是 key-value 的对数

127.0.0.1:6379> HLEN myhash
(integer) 3
  • HINCRBY key field increment

在汇合中的某个 key 上增加值,若值是负数,则是加,若值是正数则是减

  • HSETNX key field value

向 hash 中退出键值对,若不存在则增加,存在则增加失败

127.0.0.1:6379> hset myhash age 12
(integer) 1
127.0.0.1:6379> HINCRBY myhash age 2
(integer) 14
127.0.0.1:6379> HINCRBY myhash age -1
(integer) 13
127.0.0.1:6379> HSETNX myhash name xiaozhu
(integer) 1
127.0.0.1:6379> HSETNX myhash name xiaopangzi
(integer) 0
127.0.0.1:6379> hgetall myhash
1) "age"
2) "13"
3) "name"
4) "xiaopangzi"

hash 的利用场景有,常常变更数据的中央,尤其是一些用户的信息,常常变动的信息

hash 更适宜对象的存储,string 更加适宜字符串的存储

zset 有序汇合

zset 也是汇合,只是在 set 的根底上加上了一个值

  • ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member …]

向有序汇合中增加 分数 和 值,能够增加多个

  • ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

依照分数的程序排序,-inf 为负无穷,+inf 为正无穷

  • ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]

查看有序汇合外面的所有的值

127.0.0.1:6379> ZADD myage 10 xiaoming 8 xiaohong 19 xiaozhu
(integer) 3
127.0.0.1:6379> ZRANGEBYSCORE myage 0 -1
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE myage -inf +inf
1) "xiaohong"
2) "xiaoming"
3) "xiaozhu"
127.0.0.1:6379> ZRANGEBYSCORE myage -inf +inf withscores
1) "xiaohong"
2) "8"
3) "xiaoming"
4) "10"
5) "xiaozhu"
6) "19"
127.0.0.1:6379> ZRANGEBYSCORE myage -inf 10
1) "xiaohong"
2) "xiaoming"
127.0.0.1:6379> ZRANGE myage 0 -1
1) "xiaohong"
2) "xiaoming"
3) "xiaozhu"
  • ZREM key member [member …]

删除有序汇合外面的数据

127.0.0.1:6379> ZREM myage xiaoming
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE myage -inf +inf withscores
1) "xiaohong"
2) "8"
3) "xiaozhu"
4) "19"
  • ZREVRANGE key start stop [WITHSCORES]

对有序汇合从大到小排序

127.0.0.1:6379> ZADD myage 7 xiaohe 17 xiaoliu 14 huhu
(integer) 3
127.0.0.1:6379> ZRANGE myage 0 -1
1) "xiaohe"
2) "xiaohong"
3) "huhu"
4) "xiaoliu"
5) "xiaozhu"
127.0.0.1:6379> ZRANGEBYSCORE myage -1 20
1) "xiaohe"
2) "xiaohong"
3) "huhu"
4) "xiaoliu"
5) "xiaozhu"
127.0.0.1:6379> ZRANGEBYSCORE myage -1 20 withscores
 1) "xiaohe"
 2) "7"
 3) "xiaohong"
 4) "8"
 5) "huhu"
 6) "14"
 7) "xiaoliu"
 8) "17"
 9) "xiaozhu"
10) "19"
127.0.0.1:6379> ZREVRANGE myage 0 -1
1) "xiaozhu"
2) "xiaoliu"
3) "huhu"
4) "xiaohong"
5) "xiaohe"

其余的 api 咱们能够到 redis 官网上去进行学习和实际,能够查看 redis 官网的中文文档,http://www.redis.cn/

zset 的利用场景有:

set 排序,存储班级问题信息,工资信息等等,用于排序和筛选的

带有权重的数据或者音讯,排行榜的实现等等

收费支付红包封面哦,总共 2000 个 ,先到先得

欢送点赞,关注,珍藏

敌人们,你的反对和激励,是我保持分享,提高质量的能源

好了,本次就到这里

技术是凋谢的,咱们的心态,更应是凋谢的。拥抱变动,背阴而生,致力向前行。

我是小魔童哪吒,欢送点赞关注珍藏,下次见~

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年50元

阿里云限时活动-2核2G-5M带宽-60G SSD-1000G月流量 ,特惠价99元/年(原价1234.2元/年,可以直接买3年),速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据