Redis数据类型实践(三)

图片[1]|Redis数据类型实践(三)|leon的博客

1.1.1 字符串类型

1.1.1.1 创建字符串:一个键最大能存储512MB的值

[root@centos7 ~]# redis-cli -h 10.0.0.103 -p 6379
10.0.0.103:6379> auth 123456
OK
10.0.0.103:6379> set name leon
OK
10.0.0.103:6379> get name
"leon"
10.0.0.103:6379> type name
string

1.1.1.2 将value追加到key原来的值的末尾:append

10.0.0.103:6379> append name 123
(integer) 7
10.0.0.103:6379> get name
"leon123"

1.1.1.3 同时设置一个或多个键值对:mset/mget

10.0.0.103:6379> mset name leon foo shadow
OK
10.0.0.103:6379> get foo
"shadow"
10.0.0.103:6379> get name
"leon"
10.0.0.103:6379> mget name foo
1) "leon"
2) "shadow"

1.1.1.4 计算字符长度:strlen

10.0.0.103:6379> strlen name
(integer) 4

1.1.1.5 检查是否存在key

10.0.0.103:6379> exists foo
(integer) 1

1.1.1.6 删除key:del

10.0.0.103:6379> del foo
(integer) 1
10.0.0.103:6379> exists foo
(integer) 0

1.1.2 整数类型

1.1.2.1 自增和自减1:incr/decr

10.0.0.103:6379> set age 33
OK
10.0.0.103:6379> incr age
(integer) 34
10.0.0.103:6379> decr age
(integer) 33

1.1.2.2 自增和自减指定值:incrby/decrby

10.0.0.103:6379> incrby age 100
(integer) 133
10.0.0.103:6379> decrby age 100
(integer) 33

1.1.3 Hash类型

Redis Hash是一个键值对集合(string类型的field和value的映射表),Hash特别适用于存储对象,每个Hash可以存储2^32-1个键值对。

1.1.3.1 创建hash类型

10.0.0.103:6379> hset user:1 name leon
(integer) 1
10.0.0.103:6379> hget user:1 name
"leon"
10.0.0.103:6379> type user:1
hash

1.1.3.2 批量创建和获取Hash:hmset/hmget/hgetall

10.0.0.103:6379> hmset user:1 age 33 sex male
OK
10.0.0.103:6379> hmget user:1 name age
1) "leon"
2) "33"
10.0.0.103:6379> hgetall user:1
1) "name"
2) "leon"
3) "age"
4) "33"
5) "sex"
6) "male"

1.1.3.3 删除某个字段:hdel

10.0.0.103:6379> hdel user:1 sex
(integer) 1
10.0.0.103:6379> hgetall user:1
1) "name"
2) "leon"
3) "age"
4) "33"

1.1.4 list类型

Redis列表是简单的字符串列表,按照插入顺序排序,每个列表可以存储2^32-1个键值对。

1.1.4.1 创建list:lpush

10.0.0.103:6379> lpush list1 ly leon shadow
(integer) 3
10.0.0.103:6379> type list1
list

1.1.4.2 遍历列表:lrange

10.0.0.103:6379> lrange list1 0 10
1) "shadow"
2) "leon"
3) "ly"

1.1.4.3 插入数据到list:lpush/rpush

10.0.0.103:6379> lpush list1 liyang     # 插入到开始
(integer) 4
10.0.0.103:6379> rpush list1 byd        # 插入到末尾
(integer) 5
10.0.0.103:6379> lrange list1 0 10
1) "liyang"
2) "shadow"
3) "leon"
4) "ly"
5) "byd"

1.1.4.4 从list移除(取出)数据:rpop/lpop

10.0.0.103:6379> rpop list1             # 移除(取出)末尾数据
"byd"
10.0.0.103:6379> lrange list1 0 10
1) "liyang"
2) "shadow"
3) "leon"
4) "ly"
10.0.0.103:6379> lpop list1             # 移除(取出)开头数据
"liyang"
10.0.0.103:6379> lrange list1 0 10
1) "shadow"
2) "leon"
3) "ly"

1.1.4.5 获取列表长度:llen

10.0.0.103:6379> llen list1
(integer) 3

1.1.4.6 删除列表数据:lrem

10.0.0.103:6379> lrem list1 1 leon
(integer) 1
10.0.0.103:6379> lrange list1 0 10
1) "shadow"
2) "ly"

1.1.4.7 更改列表值:lset

10.0.0.103:6379> lrange list1 0 10
1) "shadow"
2) "ly"
10.0.0.103:6379> lset list1 0 leon
OK
10.0.0.103:6379> lrange list1 0 10
1) "leon"
2) "ly"

1.1.4.8 按照下标返回列表值:lindex

10.0.0.103:6379> lindex list1 0
"leon"

1.1.4.9 在列表中插入一个值:linsert

10.0.0.103:6379> linsert list1 after leon shadow
(integer) 3
10.0.0.103:6379> lrange list1 0 10
1) "leon"
2) "shadow"
3) "ly"

1.1.5 set无序集合类型

Redis的Set是string类型的无序集合,集合的成员是唯一的,意味着集合中不能出现重复的数据,Redis中集合是通过哈希表实现的。

1.1.5.1 将数据加入到集合(已经存在的元素会被忽略并变更到最后位置):sadd

10.0.0.103:6379> sadd set1 zhao qian sun li zhou wu zheng wang
(integer) 8
10.0.0.103:6379> smembers set1
1) "li"
2) "sun"
3) "wu"
4) "zhou"
5) "wang"
6) "zheng"
7) "zhao"
8) "qian"
10.0.0.103:6379> type set1
set

1.1.5.2 获取集合长度:scard

10.0.0.103:6379> scard set1
(integer) 8

1.1.5.3 返回集合的差集:sdiff

10.0.0.103:6379> smembers set2
1) "zhou"
2) "wu"
3) "zheng"
4) "wang"
5) "liu"
10.0.0.103:6379> sadd set2 zhou wu zheng wang liu
(integer) 5
10.0.0.103:6379> sdiff set1 set2        # set1比set2集合中多的部分
1) "sun"
2) "zhao"
3) "qian"
4) "li"
10.0.0.103:6379> sdiff set2 set1        # set2比set1集合中多的部分
1) "liu"

1.1.5.4 返回集合的并集:sunion

10.0.0.103:6379> sunion set1 set2
1) "qian"
2) "sun"
3) "li"
4) "wang"
5) "zheng"
6) "zhao"
7) "zhou"
8) "wu"
9) "liu"

1.1.5.5 返回集合的交集:sinter

10.0.0.103:6379> sinter set1 set2
1) "zhou"
2) "wu"
3) "zheng"
4) "wang"

1.1.5.6 判断是否属于该集合:sismember

10.0.0.103:6379> sismember set1 li
(integer) 1

1.1.5.7 集合内元素的移动:smove

10.0.0.103:6379> smove set1 set2 sun
(integer) 1
10.0.0.103:6379> smembers set1
1) "li"
2) "zheng"
3) "wang"
4) "zhao"
5) "qian"
6) "zhou"
7) "wu"
10.0.0.103:6379> smembers set2
1) "sun"
2) "zhou"
3) "wu"
4) "zheng"
5) "wang"
6) "liu"

1.1.5.8 从头移出(取出)一个元素:spop

10.0.0.103:6379> spop set1 1
1) "li"
10.0.0.103:6379> smembers set1
1) "zheng"
2) "wang"
3) "zhao"
4) "qian"
5) "zhou"
6) "wu"

1.1.5.9 读取一个随机元素:srandmember

10.0.0.103:6379> srandmember set2 1
1) "zheng"
10.0.0.103:6379> smembers set2
1) "sun"
2) "zhou"
3) "wu"
4) "zheng"
5) "wang"
6) "liu"

1.1.5.10 删除一个或多个元素:srem

10.0.0.103:6379> srem set1 zhou
(integer) 1
10.0.0.103:6379> smembers set1
1) "zheng"
2) "wang"
3) "zhao"
4) "qian"
5) "wu"

1.1.6 SortedSet有序集合类型

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

1.1.6.1 创建有序集合

10.0.0.103:6379> zadd sset1 5000 ly 6000 leon 7000 shadow 10000 liyang 1000 dage
(integer) 5
10.0.0.103:6379> zrangebyscore sset1 1 10000
1) "dage"
2) "ly"
3) "leon"
4) "shadow"
5) "liyang"
10.0.0.103:6379> zrangebyscore sset1 -inf +inf WITHSCORES
1) "dage"
2) "1000"
3) "ly"
4) "5000"
5) "leon"
6) "6000"
7) "shadow"
8) "7000"
9) "liyang"
10) "10000"
10.0.0.103:6379> type sset1
zset

1.1.6.2 返回有序集key的基数:zcard

10.0.0.103:6379> zcard sset1
(integer) 5

1.1.6.3 计算score在2000到8000之间的数:zcount

10.0.0.103:6379> zcount sset1 2000 8000
(integer) 3

1.1.6.4 获取集合中成员的score:zscore

10.0.0.103:6379> zscore sset1 shadow
"7000"

1.1.6.5 返回指定区间(排名)成员:zrange

# 正向排序
10.0.0.103:6379> zrange sset1 0 -1 WITHSCORES
1) "dage"
2) "1000"
3) "ly"
4) "5000"
5) "leon"
6) "6000"
7) "shadow"
8) "7000"
9) "liyang"
10) "10000"
10.0.0.103:6379> zrange sset1 0 2
1) "dage"
2) "ly"
3) "leon"
# 逆向排序
10.0.0.103:6379> zrevrange sset1 0 -1
1) "liyang"
2) "shadow"
3) "leon"
4) "ly"
5)"dage"

1.1.6.6 显示排名:zrank

10.0.0.103:6379> zrank sset1 shadow
(integer) 3

1.1.6.7 移出一个或多个成员:zrem

10.0.0.103:6379> zrem sset1 dage
(integer) 1
10.0.0.103:6379> zrange sset1 0 -1
1) "ly"
2) "leon"
3) "shadow"
4) "liyang"

1.1.6.8 为成员的score设置增量(负数为减法):zincrby

10.0.0.103:6379> zincrby sset1 1000 shadow
"8000"
温馨提示:本文最后更新于2022-12-20 20:57:50,已超过431天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!
转载请注明本文链接:https://blog.leonshadow.cn/763482/885.html
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享