当前位置:网站首页>[Redis] Redis Learning - Five Basic Data Types
[Redis] Redis Learning - Five Basic Data Types
2022-08-07 00:05:00 【Cxj__】
【Redis】Redis学习——五大基本数据类型
前言
Redis:REmote DIctionary Server(远程字典服务器)是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能(Key/Value)分布式内存数据库,基于内存运行,并支持持久化的NoSQL数据库,是当前最热门的NoSQL数据库之一.它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型.
一、字符串(String)
特点:一个key对应一个value,value可以是string也可以为数字
string是 Redis最基本的类型,一个key对应一个value.Redis string 类型是二进制安全的.即Redis的string可以包含任何数据,比如jpg图片或者序列化的对象 .Redis stringType a key can store the maximum512MB 数据.
| 命令 | 描述 |
|---|---|
| SET KEY VALUE | 设置指定 KEY 的值 |
| GET KEY | 获取指定 KEY 的值 |
| GETRANGE KEY START END | 返回 KEY 中字符串值的子字符 |
| GETSET KEY VALUE | 将给定 KEY 的值设为 VALUE ,并返回 KEY 的旧值 |
| GETBIT KEY OFFSET | 对 KEY 所储存的字符串值,获取指定偏移量上的位 |
| MGET KEY1 [KEY2…] | 获取所有(一个或多个)给定 KEY 的值 |
| SETBIT KEY OFFSET VALUE | 对 KEY 所储存的字符串值,设置或清除指定偏移量上的位 |
| SETEX KEY seconds VALUE | 将值 VALUE 关联到 KEY ,并将 KEY 的过期时间设为 seconds (以秒为单位) |
| SETNX KEY VALUE | 只有在 KEY 不存在时设置 KEY 的值 |
| SETRANGE KEY offset VALUE | 用 VALUE 参数覆写给定 KEY 所储存的字符串值,从偏移量 offset 开始 |
| STRLEN KEY | 返回 KEY 所储存的字符串值的长度 |
| MSET KEY VALUE KEY VALUE … | 同时设置一个或多个 KEY-VALUE 对 |
| MSETNX KEY VALUE KEY VALUE … | 同时设置一个或多个 KEY-VALUE 对,当且仅当所有给定 KEY 都不存在 |
| PSETEX KEY milliseconds VALUE | 这个命令和 SETEX 命令相似,但它以毫秒为单位设置 KEY 的生存时间 |
| INCR KEY | 将 KEY 中储存的数字值增一 |
| INCRBY KEY increment | 将 KEY 所储存的值加上给定的增量值 increment |
| INCRBYFLOAT KEY increment | 将 KEY 所储存的值加上给定的浮点增量值 increment |
| DECR KEY | 将 KEY 中储存的数字值减一 |
| DECRBY KEY decrement | KEY 所储存的值减去给定的减量值 decrement |
| APPEND KEY VALUE | 如果 KEY 已经存在并且是一个字符串, APPEND 命令将指定的 VALUE 追加到该 KEY 原来值的末尾 |
| BITCOUNT KEY START END | 计算给定字符串中,被设置为 1 的比特位的数量 |
| BITOP OPERATION DESTKEY KEY KEY … | 对二进制位进行操作 |
127.0.0.1:6379> set name chandler #设定键name值
OK
127.0.0.1:6379> get name #获得键name的值
"chandler"
127.0.0.1:6379> getrange name 0 -1 #获取nameValue substring,0代表字符串开头,-1Represents the end of the string
"chandler"
127.0.0.1:6379> getrange name 0 3 #获取索引0-3的子字符串
"chan"
127.0.0.1:6379> getset name monica #先get后set
"chandler"
127.0.0.1:6379> get name
"monica"
127.0.0.1:6379> mset friend1 ross friend2 rachel #Set multiple key-values at the same time
OK
127.0.0.1:6379> mget friend1 friend2 #同时获取多个键值
1) "ross"
2) "rachel"
127.0.0.1:6379> set word "hello"
OK
127.0.0.1:6379> append word " world" #Append a string to the string value
(integer) 11
127.0.0.1:6379> get word
"hello world"
127.0.0.1:6379> strlen word #获取键word的值的长度
(integer) 11
127.0.0.1:6379> setrange word 6 linux #从索引为6used after the element“linux”进行覆盖
(integer) 11
127.0.0.1:6379> get word
"hello linux"
127.0.0.1:6379> set age 30
OK
127.0.0.1:6379> incr age #让数值增加1
(integer) 31
127.0.0.1:6379> incrby age 5 #数值增加5
(integer) 36
127.0.0.1:6379> decrby age 10 #数值减少10
(integer) 26
127.0.0.1:6379> decr age #数值减少1
(integer) 25
127.0.0.1:6379> setex friend3 30 joey #设置键friends3的生存时间为30s,值为joey
OK
127.0.0.1:6379> ttl friend3
(integer) 26
127.0.0.1:6379> setnx age 30 #age存在,No value will be set
(integer) 0
127.0.0.1:6379> get age
"25"
127.0.0.1:6379> setnx age2 30 #age2不存在,Set the value toage2
(integer) 1
127.0.0.1:6379> get age2
"30"
二、列表(List)
特点:一个key对应多个value
Redis的List是简单的字符串列表,按照插入顺序排序.Redis的list是每个子元素都是String类型的双向链表,可以通过push和pop操作从列表的头部或者尾部添加或者删除元素,这样List即可以作为栈,也可以作为队列.ListOne application is message queues,可以利用List的PUSH操作,将任务存在List中,然后工作线程再用POP操作将任务取出进行执行.Redis还提供了操作List中某一段的api,你可以直接查询,删除List中某一段的元素.
| 命令 | 描述 |
|---|---|
| LPUSH KEY value1 value2 | 将一个或多个值插入到列表头部. |
| LPUSHX KEY value | 将一个值插入到已存在的列表头部. |
| RPUSH KEY value1 value2 | 将一个或多个值插入到列表尾部. |
| RPUSHX KEY value | 将一个值插入到已存在的列表尾部. |
| LSET KEY index value | 将列表索引 index 位置的值设置为 value. |
| LINSERT KEY BEFORE AFTER pivot value | 将值 value 插入到列表 KEY 当中,位于值 pivot 之前或之后. |
| LPOP KEY | 获取并移除列表的第一个元素. |
| RPOP KEY | 获取并移除列表的最后一个元素. |
| BLPOP KEY1 KEY2 timeout | 获取并移除列表的第一个元素, Blocks the list if the list has no elements until it times out or an element can be popped. |
| BRPOP KEY1 KEY2 timeout | 获取并移除列表的最后一个元素, Blocks the list if the list has no elements until it times out or an element can be popped. |
| RPOPLPUSH source destination | 移除 source 列表的最后一个元素,并将该元素添加到另一个列表 destination start and return. |
| BRPOPLPUSH source destination timeout | 1. BRPOPLPUSH 是 RPOPLPUSH 的阻塞版本.2. 当 source 有数据时,BRPOPLPUSH 的表现与 RPOPLPUSH 完全一样.3. 当 source 是空时,Blocks the list until it times out or there are elements to pop. |
| LLEN KEY | 获取列表长度. |
| LINDEX KEY index | 通过索引获取列表中的元素. |
| LREM KEY count value | 从列表中移除 count 个值与 value 相等的元素. |
| LTRIM KEY start stop | 对一个列表进行修剪(trim),Only keep the ones in the list start 和 stop 之间的元素. |
| LRANGE KEY start stop | 获取列表 start 和 stop 之间 的元素. |
127.0.0.1:6379> lpush friends chandler #在列表friends头部插入chandler
(integer) 1
127.0.0.1:6379> lpush friends monica joey #在列表friendsThe heads are inserted one after the other at the same timemonica joey
(integer) 3
127.0.0.1:6379> lrange friends 0 -1 #查看索引为0的元素到最后一个元素
1) "joey"
2) "monica"
3) "chandler"
127.0.0.1:6379> rpush friends phoebe #在列表friends尾部插入phoebe
(integer) 4
127.0.0.1:6379> lrange friends 0 -1
1) "joey"
2) "monica"
3) "chandler"
4) "phoebe"
127.0.0.1:6379> rpush friends ross rachel #在列表friends尾部依次插入ross rachel
(integer) 6
127.0.0.1:6379> lrange friends 0 -1
1) "joey"
2) "monica"
3) "chandler"
4) "phoebe"
5) "ross"
6) "rachel"
127.0.0.1:6379> linsert friends before joey alan #在列表friends中joey元素之前插入alan
(integer) 7
127.0.0.1:6379> lrange friends 0 -1
1) "alan"
2) "joey"
3) "monica"
4) "chandler"
5) "phoebe"
6) "ross"
7) "rachel"
127.0.0.1:6379> linsert friends after joey bob #在列表friends中joey元素之后插入bob
(integer) 8
127.0.0.1:6379> lrange friends 0 -1
1) "alan"
2) "joey"
3) "bob"
4) "monica"
5) "chandler"
6) "phoebe"
7) "ross"
8) "rachel"
127.0.0.1:6379> lset friends 2 cindy #将列表索引为2的元素值改为cindy
OK
127.0.0.1:6379> lrange friends 0 -1
1) "alan"
2) "joey"
3) "cindy"
4) "monica"
5) "chandler"
6) "phoebe"
7) "ross"
8) "rachel"
127.0.0.1:6379> lpop friends #Pops an element at the head of the list
"alan"
127.0.0.1:6379> lrange friends 0 -1
1) "joey"
2) "cindy"
3) "monica"
4) "chandler"
5) "phoebe"
6) "ross"
7) "rachel"
127.0.0.1:6379> lpop friends 3 #The header of the list pops up3个元素
1) "joey"
2) "cindy"
3) "monica"
127.0.0.1:6379> lrange friends 0 -1
1) "chandler"
2) "phoebe"
3) "ross"
4) "rachel"
127.0.0.1:6379> rpop friends 2 #The end of the list pops up2个元素
1) "rachel"
2) "ross"
127.0.0.1:6379> lrange friends 0 -1
1) "chandler"
2) "phoebe"
127.0.0.1:6379> llen friends #查看列表friends的长度
(integer) 2
127.0.0.1:6379> lindex friends 1 #查看索引为1的成员
"phoebe"
127.0.0.1:6379> rpush friends alan bob cindy bob nancy bob
(integer) 8
127.0.0.1:6379> lrem friends 2 bob #移除列表中2和bob元素
(integer) 2
127.0.0.1:6379> lrange friends 0 -1
1) "chandler"
2) "phoebe"
3) "alan"
4) "cindy"
5) "nancy"
6) "bob"
127.0.0.1:6379> ltrim friends 3 5 #保留列表索引3-5元素,rest truncation
OK
127.0.0.1:6379> lrange friends 0 -1
1) "cindy"
2) "nancy"
3) "bob"
三、哈希(Hash)
特点:一个key对应一个value,此valueContains one or more key-value pairs(filed-value)
Redis hash是一个键值对集合.Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象.每个hash可以存储 2^32 - 1个键值对
| 命令 | 描述 |
|---|---|
| HSET KEY field value | 将哈希表 KEY 中的字段 field 的值设为 value . |
| HSETNX KEY field value | 只有在字段 field 不存在时,设置哈希表字段的值. |
| HMSET KEY field1 value1 field2 value2 | 同时将多个 field-value (域-值)对设置到哈希表 KEY 中. |
| HGET KEY field | 获取存储在哈希表中指定字段的值. |
| HGETALL KEY | 获取在哈希表中指定 KEY 的所有字段和值. |
| HMGET KEY field1 field2 | 获取所有给定字段的值. |
| HKEYS KEY | 获取所有哈希表中的字段. |
| HVALS KEY | 获取哈希表中所有值. |
| HLEN KEY | 获取哈希表中字段的数量. |
| HINCRBY KEY field increment | 为哈希表 KEY 中的指定字段的整数值加上增量 increment . |
| HINCRBYFLOAT KEY field increment | 为哈希表 KEY 中的指定字段的浮点数值加上增量 increment . |
| HDEL KEY field1 field2 | 删除一个或多个哈希表字段. |
| HEXISTS KEY field | 查看哈希表 KEY 中,指定的字段是否存在. |
| HSCAN KEY cursor [MATCH pattern] [COUNT count] | 迭代哈希表中的键值对,类似 SCAN 命令. |
127.0.0.1:6379> hset user name chandler #设置user(key)的name(field)值为chandler
(integer) 1
127.0.0.1:6379> hget user name #获取user(key)的name(field)值
"chandler"
127.0.0.1:6379> hmset user1 name rachel age 30 #同时设置user1(key)的name、age(field)值
OK
127.0.0.1:6379> hmget user1 name age #同时获取user1(key)的name、age(field)值
1) "rachel"
2) "30"
127.0.0.1:6379> hkeys user #获取user的所有keys
1) "name"
127.0.0.1:6379> hkeys user1
1) "name"
2) "age"
127.0.0.1:6379> hvals user1 #获取user1所有field值
1) "rachel"
2) "30"
127.0.0.1:6379> hlen user1 #获取user1的字段数量
(integer) 2
127.0.0.1:6379> hincrby user1 age 3 #键user1的age字段值增加3
(integer) 33
127.0.0.1:6379> hdel user1 name age #键user1的name、age字段删除
(integer) 2
127.0.0.1:6379> hkeys user1
(empty array)
127.0.0.1:6379> hexists user1 age #查看user1的age字段是否存在
(integer) 0
127.0.0.1:6379> hexists user name
(integer) 1
四、集合(Set)
特点:一个key对应多个value,成员不重复
Redis的Set是string类型的无序集合.集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1).集合中最大的成员数为 2^32 - 1.
| 命令 | 描述 |
|---|---|
| SADD key member1 member2 | 向集合添加一个或多个成员(成员不重复) |
| SCARD key | 获取集合的成员数. |
| SDIFF key1 key2 | 返回给定所有集合的差集. |
| SDIFFSTORE destination key1 key2 | 返回给定所有集合的差集并存储在 destination 中. |
| SINTER key1 key2 | 返回给定所有集合的交集. |
| SINTERSTORE destination key1 key2 | 返回给定所有集合的交集并存储在 destination 中. |
| SISMEMBER key member | 判断 member 元素是否是集合 key 的成员. |
| SMEMBERS key | 返回集合中的所有成员. |
| SMOVE source destination member | 将 member 元素从 source 集合移动到 destination 集合. |
| SPOP key | 移除并返回集合中的一个随机元素. |
| SRANDMEMBER key count | 返回集合中一个或多个随机数. |
| SREM key member1 member2 | 移除集合中一个或多个成员. |
| SUNION key1 key2 | 返回所有给定集合的并集. |
| SUNIONSTORE destination key1 key2 | 所有给定集合的并集存储在 destination 集合中. |
| SSCAN key cursor MATCH pattern COUNT count | 迭代集合中的元素. |
127.0.0.1:6379> sadd group1 monica chandler phoebe joey #向集合group2中插入monica chandler phoebe joey
(integer) 4
127.0.0.1:6379> sadd group2 phoebe ross rachel joey
(integer) 4
127.0.0.1:6379> scard group1 #查看集合group1中成员个数
(integer) 4
127.0.0.1:6379> sdiff group1 group2 #查看group1相对于group2的差集
1) "monica"
2) "chandler"
127.0.0.1:6379> sdiff group2 group1 #查看group2相对于group1的差集
1) "ross"
2) "rachel"
127.0.0.1:6379> sinter group1 group2 #查看group1与group2的交集
1) "joey"
2) "phoebe"
127.0.0.1:6379> sunion group1 group2 #查看group1与group2的并集
1) "ross"
2) "monica"
3) "phoebe"
4) "rachel"
5) "joey"
6) "chandler"
127.0.0.1:6379> smove group1 group2 monica #将group1(source)中的monica移动到group2(destination)
(integer) 1
127.0.0.1:6379> smembers group1 #查看group1中的所有成员
1) "joey"
2) "phoebe"
3) "chandler"
127.0.0.1:6379> sismember group2 monica #查看group2contains membersmonica
(integer) 1
127.0.0.1:6379> smembers group2
1) "joey"
2) "phoebe"
3) "ross"
4) "monica"
5) "rachel"
127.0.0.1:6379> spop group2 2 #随机从group2removes and returns two elements from
1) "phoebe"
2) "monica"
127.0.0.1:6379> smembers group2
1) "ross"
2) "rachel"
3) "joey"
127.0.0.1:6379> srandmember group2 2 #随机从group2Returns two elements in (不移除)
1) "ross"
2) "joey"
127.0.0.1:6379> smembers group2
1) "ross"
2) "rachel"
3) "joey"
127.0.0.1:6379> srem group2 ross joey #删除group2中ross joey成员
(integer) 2
127.0.0.1:6379> smembers group2
1) "rachel"
127.0.0.1:6379>
五、有序集合(Sorted Set)
特点:一个key对应多个value,在setBased on the addition of a for sortingscore
Redis的zset和set一样也是 string 类型元素的集合,且不允许重复的成员.不同的是每个元素都会关联一个double类型的分数.RedisIt is by scores that the members of the set are sorted from smallest to largest.zset 的成员是唯一的,但分数(score)却可以重复.
| 命令 | 描述 |
|---|---|
| ZADD KEY score1 member1 [score2 member2] | 向有序集合添加一个或多个成员,或者更新已存在成员的分数. |
| ZCARD KEY | 获取有序集合的成员数. |
| ZCOUNT KEY min max | 计算在有序集合中指定区间分数的成员数. |
| ZINCRBY KEY increment member | 有序集合中对指定成员的分数加上增量 increment . |
| ZINTERSTORE destination num KEY [KEY …] | 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 KEY 中. |
| ZLEXCOUNT KEY min max | 在有序集合中计算指定字典区间内成员数量. |
| ZRANGE KEY start stop [WITHSCORES] | 通过索引区间返回有序集合成指定区间内的成员. |
| ZRANGEBYLEX KEY min max [LIMIT offset count] | 通过字典区间返回有序集合的成员. |
| ZRANGEBYSCORE KEY min max [WITHSCORES] [LIMIT] | 通过分数返回有序集合指定区间内的成员. |
| ZRANK KEY member | 返回有序集合中指定成员的索引. |
| ZREM KEY member [member …] | 移除有序集合中的一个或多个成员. |
| ZREMRANGEBYLEX KEY min max | 移除有序集合中给定的字典区间的所有成员. |
| ZREMRANGEBYRANK KEY start stop | 移除有序集合中给定的排名区间的所有成员. |
| ZREMRANGEBYSCORE KEY min max | 移除有序集合中给定的分数区间的所有成员. |
| ZREVRANGE KEY start stop [WITHSCORES] | 返回有序集中指定区间内的成员,通过索引,分数从高到底. |
| ZREVRANGEBYSCORE KEY max min [WITHSCORES] | 返回有序集中指定分数区间内的成员,分数从高到低排序. |
| ZREVRANK KEY member | 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序. |
| ZSCORE KEY member | 返回有序集中,成员的分数值. |
| ZUNIONSTORE destination numKEYs KEY [KEY …] | 计算给定的一个或多个有序集的并集,并存储在新的 KEY 中. |
| ZSCAN KEY cursor [MATCH pattern] [COUNT count] | 迭代有序集合中的元素(包括元素成员和元素分值). |
127.0.0.1:6379> zadd friends 1 chandler 3 monica 5 phoebe 7 joey 8 ross 9 rachel#Add members and corresponding to the sorted set respectivelyscore
(integer) 6
127.0.0.1:6379> zcard friends #查看friends中成员个数
(integer) 6
127.0.0.1:6379> zcount friends -inf +inf #计算scoreThe number of members from negative infinity to positive infinity
(integer) 6
127.0.0.1:6379> zcount friends 3 8
(integer) 4
127.0.0.1:6379> zrange friends 2 6 #返回索引在2-6之间的成员
1) "phoebe"
2) "joey"
3) "ross"
4) "rachel"
127.0.0.1:6379> zrank friends monica #返回friends中成员monica的索引
(integer) 1
127.0.0.1:6379> zrem friends monica chandler #移除成员monica chandler
(integer) 2
127.0.0.1:6379> zscore friends joey #查看成员joey的score
"7"
127.0.0.1:6379> zrange friends 0 10
1) "phoebe"
2) "joey"
3) "ross"
4) "rachel"
127.0.0.1:6379> zrank friends ross
(integer) 2
127.0.0.1:6379> zincrby friends 3 phoebe #对成员phoebe的score增加3
"8"
127.0.0.1:6379> zscore friends phoebe
"8"
127.0.0.1:6379> zrangebyscore friends -inf +inf withscores #Returns a list of members in increasing order
1) "joey"
2) "7"
3) "phoebe"
4) "8"
5) "ross"
6) "8"
7) "rachel"
8) "9"
总结
本文整理了RedisThe use of five basic data structures,分别是字符串String、列表List、哈希Hash、集合Set、有序集合ZSet.Introduces the common commands of five data types and how to use them.
参考资料
边栏推荐
猜你喜欢

bugku 这是一个单纯的图片

头像上传功能

leetcode 20. 有效的括号

图注意力机制理解

Usage and simulation implementation of library functions such as strcmp, strstr, memcpy, and memmove

A Preliminary Study of Web Development: The Box Model of Web Page Layout

bugku easy_nbt

抓取商品上传提示“图片宽度不能大于5000,长度不能大于5000,请修改!”,怎么解决?

性能提升400倍丨外汇掉期估值计算优化案例

Bugku this is a simple picture
随机推荐
leetcode 21. 合并两个有序链表(可进阶)
bugku 0和1的故事
博士也会毕业吗?
js中的原型与原型链
学习jsEs6中Symbol
leetcode 24. 两两交换链表中的节点
安静的思考
P4315 月下“毛景树”(树链剖分)
【使用JDBC获取数据库相关的元数据信息】
Promise的点点滴滴
手撕 Android Framework底层面试题集合
strcmp、strstr、memcpy、memmove等库函数的用法和模拟实现
vi学习(2)【常用命令包括移动光标/选中文本/撤销和反撤销/删除文本/复制粘贴/查找替换/插入】
在中国银河证券开户安全吗
常用邮件服务器支持端口及加密方法实测
多项式——乘法逆元
[7] Advanced C language -- program compilation (preprocessing operation) + linking
月薪8万炫富后续来了,金融业“限薪令”重磅出击
重建二叉树
logcat: Solution for Unexpected EOF!