Redis模块学习(3)---Redis基本操作

Redis 的五种基本类型

 

  书接上文的STRING,本文主要说明其他类型的使用:

1)LIST(列表)类型:

list的value是一个序列集合且每个节点都包好了一个元素,能够对序列执行push、pop、修改、查找和移除等。一个list最多容纳2^32-1个元素,包括两端添加、查看列表、两端弹出、获取列表元素个数和一些扩展操作

 

 实际应用:

127.0.0.1:6379> lpush mylist vi v2  //从左边添加,从开头加
(integer) 2
127.0.0.1:6379> lrange mylist 0 1  //从左边(开头)读取序号为0到1的数据
1) "v2"
2) "vi"
127.0.0.1:6379> lpush mylist v11 //单独加。从左边加
(integer) 3
127.0.0.1:6379> lrange mylist 0 1 //从头开始打印0到1的数值
1) "v11"
2) "v2"
127.0.0.1:6379> lrange mylist 0 2
1) "v11"
2) "v2"
3) "vi"
127.0.0.1:6379> lrange mylist 0 -1 //从0开始到倒数最后一个
1) "v11"
2) "v2"
3) "vi"
127.0.0.1:6379> rpush mylist v12 //从末尾开始追加
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1 
1) "v11"
2) "v2"
3) "vi"
4) "v12"
127.0.0.1:6379> llen mylist //打印长度
(integer) 4
127.0.0.1:6379> lindex mylist 1 //使用索引读取
"v2"
127.0.0.1:6379> ltrim mylist 1 2 //截取1到2的值并赋值
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "v2"
2) "vi"
127.0.0.1:6379> lpush mylist v3
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "v3"
2) "v2"
3) "vi"
127.0.0.1:6379> rpush mylist rv4
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "v3"
2) "v2"
3) "vi"
4) "rv4"
127.0.0.1:6379> lset mylist 1 haha  //对索引1的赋值
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "v3"
2) "haha"
3) "vi"
4) "rv4"
127.0.0.1:6379> lrem mylist 1 haha //删除1个为哈哈的值
(integer) 1
127.0.0.1:6379> lrange mylist 0 -1
1) "v3"
2) "vi"
3) "rv4"

127.0.0.1:6379> lpop mylist //从头开始删除并打印
"v3"
127.0.0.1:6379> lrange mylist 0 -1
1) "vi"
2) "rv4"

2)set(集合)类型

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

集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。

 实际应用:

127.0.0.1:6379> sadd myset1 0  //往集合myset1myset1添加元素
(integer) 1
 
127.0.0.1:6379> sadd myset1 1 2 3  //多个增加
(integer) 3
127.0.0.1:6379> smembers myset1 //查看集合里面的所有元素
1) "0"
2) "1"
3) "2"
4) "3"
127.0.0.1:6379> scard myset1 //查看集合的总个数
(integer) 4
127.0.0.1:6379> sismember myset1 3 //检测集合是否含有3的元素
(integer) 1
127.0.0.1:6379> sismember myset1 5
(integer) 0
127.0.0.1:6379> srandmember myset1 //随机打印集合元素
"0"
127.0.0.1:6379> srandmember myset1 
"1"
127.0.0.1:6379> srandmember myset1 
"0"
127.0.0.1:6379> srandmember myset1 
"3"
127.0.0.1:6379> sadd myset1 5 6 7 8  
(integer) 4
127.0.0.1:6379> smembers myset1
1) "0"
2) "1"
3) "2"
4) "3"
5) "5"
6) "6"
7) "7"
8) "8"
127.0.0.1:6379> sadd myset2 3 4 9 10
(integer) 4
127.0.0.1:6379> sinter myset1 myset2  //交集
1) "3"
127.0.0.1:6379> sinterstore myset3 myset1 myset2 //交集保存到myset3
(integer) 1
127.0.0.1:6379> smembers myset3
1) "3"
127.0.0.1:6379> sunion myset1 myset2 //并集
 1) "0"
 2) "1"
 3) "2"
 4) "3"
 5) "4"
 6) "5"
 7) "6"
 8) "7"
 9) "8"
10) "9"
11) "10"
127.0.0.1:6379> sdiff myset1 myset2 //差集
1) "0"
2) "1"
3) "2"
4) "5"
5) "6"
6) "7"
7) "8"
127.0.0.1:6379> sdiff myset2 myset1
1) "4"
2) "9"
3) "10"

127.0.0.1:6379> srem myset1 0 //删除

(integer) 1

 3) HASH(字典)类型

Redis hash 是一个键值(key=>value)对集合。

Redis hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。

 

 

 实际应用:

127.0.0.1:6379> hset mhash1 key1 value1  //添加元素
(integer) 1
127.0.0.1:6379> hget mhash1 key1 //通过键获取值
"value1"
127.0.0.1:6379> hexists mhash1 key1 //查看键是否存在
(integer) 1
127.0.0.1:6379> hexists mhash1 key2
(integer) 0
127.0.0.1:6379> hset mhash1 key2 value2 
(integer) 1
127.0.0.1:6379> hlen mhash1  //查看长度
(integer) 2
127.0.0.1:6379> hkeys mhash1 //查看集合的建
1) "key1"
2) "key2"
127.0.0.1:6379> hvals mhash1 //查看集合的值
1) "value1"
2) "value2"

127.0.0.1:6379> hmget mhash1 key1 key2 //获取对应键的值
1) "value1"
2) "value2"
127.0.0.1:6379> hgetall mhash1 //获取集合的键值对
1) "key1"
2) "value1"
3) "key2"
4) "value2"
127.0.0.1:6379> hset hash1 key3 10
(integer) 1
127.0.0.1:6379> hgetall mhash1
1) "key1"
2) "value1"
3) "key2"
4) "value2"
127.0.0.1:6379> hset mhash1 key3 10
(integer) 1
127.0.0.1:6379> hgetall mhash1
1) "key1"
2) "value1"
3) "key2"
4) "value2"
5) "key3"
6) "10"

127.0.0.1:6379> hincrby mhash1 key3 5 //对 key3的值增加5
(integer) 15
127.0.0.1:6379> hgetall mhash1
1) "key1"
2) "value1"
3) "key2"
4) "value2"
5) "key3"
6) "15"
7) "keys"
8) "5"
127.0.0.1:6379> hdel mhash1 keys  //删除集合的键值对
(integer) 1
127.0.0.1:6379> hgetall mhash1     
1) "key1"
2) "value1"
3) "key2"
4) "value2"
5) "key3"
6) "15"
127.0.0.1:6379> del mhash1 //删除集合
(integer) 1

4)zset(sorted set:有序集合)类型

Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。

zset的成员是唯一的,但分数(score)却可以重复。

 

 

 

实际应用:

127.0.0.1:6379> zadd meishi 1 mantou //添加元素
(integer) 1
127.0.0.1:6379> zadd meishi 2 baozi 3 chashao 4 shaomai //添加多个元素
(integer) 3
127.0.0.1:6379> zcard meishi  //查看长度
(integer) 4
127.0.0.1:6379> zscore meishi mantou //返回元素的权重
"1"
127.0.0.1:6379> zrank meishi mantou
(integer) 0

127.0.0.1:6379> zrange meishi 1 2 //查看部分元素
1) "baozi"
2) "chashao"

127.0.0.1:6379> zrevrange meishi 2 3  //从大到小
1) "baozi"
2) "mantou"
127.0.0.1:6379> zrange meishi 0 -1 //查看全部元素
1) "mantou"
2) "baozi"
3) "chashao"
4) "shaomai"
127.0.0.1:6379> zadd meishi 0 ganzhen
(integer) 1
127.0.0.1:6379> zrange meishi 0 -1 //查看全部元素,安装权重从小到大
1) "ganzhen"
2) "mantou"
3) "baozi"
4) "chashao"
5) "shaomai"
127.0.0.1:6379> zrem meishi baozi //删除元素
(integer) 1
127.0.0.1:6379> zrange meishi 0 -1 //查看全部元素
1) "ganzhen"
2) "mantou"
3) "chashao"
4) "shaomai"
127.0.0.1:6379> zrangebyscore meishi 2 3 //指定查看范围的元素
1) "chashao"

127.0.0.1:6379> zremrangebyscore meishi 2 3  //删除指定范围的元素
(integer) 1
127.0.0.1:6379> zrange meishi 0 -1
1) "ganzhen"
2) "mantou"
3) "shaomai"

 

参考:

https://blog.csdn.net/Lazyboy_/article/details/88821948?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-8&spm=1001.2101.3001.4242

https://blog.csdn.net/icetime17/article/details/45767559?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-2.control

posted @ 2021-06-24 18:49  沙丘蚂蚁慢慢爬  阅读(63)  评论(0编辑  收藏  举报