Redis学习笔记--Redis五种基本数据数据类型

文章目录

  • 1. Redis-Key
  • 2. Redis-List
  • 3. Redis-Set
  • 4. Redis-hash(哈希)
  • 5. Redis-Zset

Redis 版本 6.0+

Redis学习笔记--Redis五种基本数据数据类型_第1张图片

1. Redis-Key

============移动key=============
EXIST key #判断key是否存在
move key 1 #移动key到别的数据库(后面的数字是数据库名)

============设置过期时间=============
expire key 100    #设置key的过期时间 后面的数字单位是秒
tll key     #查看剩余有效时间(秒为单位)

============字符串追加内容=============
APPEND KEY XXX  #往key中追加xxx内容,如果key不存在,就是set key
STRLEN KEY  #获取string类型的字符串长度

============i++操作=============
incr key  #实现i++操作 比如用于浏览量计数
decr key  #实现i--操作
incrby key 10  #指定+=的步长,即设置增量

============截取内容=============
getrange key 0 3    #获取0到3之间的字符串内容
getrange key 0 -1   #查看全部字符串内容

============字符串替换=============
setrange key 1 xx # xx为索引位置1后修改为的内容

============带判断的set============
setex(=set with expire) #创建并设置过期时间
setnx(=set if not exist) #不存在则创建(在分布式锁中常用)


================批量操作==================
mset k1 v1 k2 v2 k3 v3 # 设置多个key和对应的value
mget key1 key2 key3 # 获取多个key的值
msetnx k1 v1 k2 v2 k3 v3 # 设置多个key的值,原子性操作,要么一起成功,要么一起失败


=================对象========================
set user:1 {name:Gangbb,age:99} # 设置user1号对象,值为json字符串
get user:1 # 获取user1号对象
# 这里的key是一个巧妙的设计: user:{id}:{filed}
mset user:1 name Gangbb user:1 age 99
mget user:1:name user:1:age
###示例:
127.0.0.1:6379[1]> mset user:2:name Gangbb user:2:age 99
OK
127.0.0.1:6379[1]> set user:1 {name:Gangbb,age:99}
OK
127.0.0.1:6379[1]> keys *
1) "user:1"
2) "user:2:age"
3) "user:2:name"
127.0.0.1:6379[1]> get user:1
"{name:Gangbb,age:99}"
127.0.0.1:6379[1]> mget user:2:name user:2:age
1) "Gangbb"
2) "99"

==============先get后set组合操作=============
getset key

####示例(原来没有mykey字段)
127.0.0.1:6379> getset mykey Gangbb   #第一次因为没有读出是(nil),所以创建mykey并写入Gangbb
(nil)
127.0.0.1:6379> getset mykey Gangbb2 #第二次读出是Gangbb,并更新写入Gangbb2
"Gangbb"
127.0.0.1:6379> get mykey #第三次只读 给果是Gangbb2
"Gangbb2"

==========================================

String类型常用场景:

  • 计数器
  • 统计多单位的数量(粉丝数、浏览数、播放量) 例:set uid:99999:follow 0
  • 对象缓存存储

2. Redis-List

在redis里面可以吧list玩成,栈、队列、阻塞队列!

所有list相关命令都是开头的:

=========================列表插入和查看========================
127.0.0.1:6379> lpush list one  # lpush 向列表的头部,添加一个或多个值
(integer) 1
127.0.0.1:6379> lpush list two  
(integer) 2
127.0.0.1:6379> lpush list three 
(integer) 3
127.0.0.1:6379> lrange list 0 -1  # lrange key 0 -1 获取key列表所有值
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrange list 0 1  # lrange key index1 index2 获取key列表索引范围的值
1) "three"
2) "two"
127.0.0.1:6379> rpush list 66666666 # rpush 向列表的尾部,添加一个或多个值
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "66666666"

======================移除列表值===================
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "66666666" 
127.0.0.1:6379> lpop list  #移除列表的第一个元素  返回移除值
"three"
127.0.0.1:6379> rpop list #移除列表的最后一个元素 返回移除值
"66666666"

======================通过index下标获取列表值============
127.0.0.1:6379> lindex list 0 #lindex 通过下标 获取list某一个值
"two"

=====================求list长度===================
127.0.0.1:6379> llen list #llen key 求列表key的长度
(integer) 2

======================移除指定的值===================
#lrem list conut value 移除list集合中指定count个数的value,精确匹配
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "one"
3) "one"
4) "three"
5) "two"
6) "one"
127.0.0.1:6379> lrem list 1 one # 从头删除1个one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "one"
3) "three"
4) "two"
5) "one"
127.0.0.1:6379> lrem list 2 one  # 从头删除2个one
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"

===================列表截断============================
#使用ltrim list index1 index2 截断list集合index1到dindex2
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "three"
4) "two"
5) "one"
127.0.0.1:6379> ltrim list 0 2 #ltrim截断0到2
OK
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "three"

================拔插组合命令=============
# rpoplpush 移动列表最后一个元素,将他移动到新的列表中
#(组合命令也是原子命令,在并发场景下使用可以提高性能)
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "three"
4) "xxx"
127.0.0.1:6379> rpoplpush list newlist
"xxx"
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "three"
127.0.0.1:6379> lrange newlist 0 -1
1) "xxx"

=============判断列表是否存在===================
127.0.0.1:6379> exists list
(integer) 1
127.0.0.1:6379> exists list2
(integer) 0

===============列表值更新替换======================
#lset 使用时如果列表不存在则会报错,存在就修改
127.0.0.1:6379> lrange newlist 0 -1
1) "xxx"
127.0.0.1:6379> lset newlist 0 newvalue
OK
127.0.0.1:6379> lrange newlist 0 -1
1) "newvalue"
127.0.0.1:6379> lset newlist2 0 newvalue  #不存在newlist2,报错
(error) ERR no such key

=============指定值前/后插入值==================
#linsert list before/after value value
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "one"
127.0.0.1:6379> linsert mylist before "one" new
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "new"
3) "one"
127.0.0.1:6379> linsert mylist after "one" new2
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "new"
3) "one"
4) "new2"


总结:

  • 实际上是一个双向链表,left、right都可以插入值
  • 如果key不存在,创建新的链表
  • 如果key存在,新增内容
  • 如果移除了列表所有值,剩一个空链表,也代表不存在!
  • 在两边插入或者改动值,效率最高!操作中间元素,相对来说效率会低一些!

3. Redis-Set

set值不能重复且无序!

===========set添加数据==============
# saad setname xx
127.0.0.1:6379> sadd myset "123"
(integer) 1
127.0.0.1:6379> sadd myset "123"
(integer) 0
127.0.0.1:6379> sadd myset "456"
(integer) 1
127.0.0.1:6379> sadd myset 999
(integer) 1

===========查看set所有数据============
# smembers
127.0.0.1:6379> smembers myset
1) "123"
2) "456"
3) "999"

============判断set中是否存在某值===============
# sismember
127.0.0.1:6379> sismember myset 999
(integer) 1
127.0.0.1:6379> sismember myset 456
(integer) 1
127.0.0.1:6379> sismember myset 111
(integer) 0

===========获取set中内容总个数===============
# scard
127.0.0.1:6379> scard myset
(integer) 3

==========移除set中某个元素==================
# srem
127.0.0.1:6379> smembers myset
1) "123"
2) "456"
3) "999"
127.0.0.1:6379> srem myset 999
(integer) 1
127.0.0.1:6379> srem myset 999
(integer) 0
127.0.0.1:6379> smembers myset
1) "123"
2) "456"


==========随机抽出set元素==============
# srandmember setname number        #number为随机抽出的个数
127.0.0.1:6379> smembers myset
1) "111"
2) "123"
3) "222"
4) "333"
5) "444"
6) "456"
127.0.0.1:6379> srandmember myset
"456"
127.0.0.1:6379> srandmember myset
"333"
127.0.0.1:6379> srandmember myset 2
1) "111"
2) "123"


============删除set中随机元素==================
# spop
127.0.0.1:6379> smembers myset
1) "111"
2) "123"
3) "222"
4) "333"
5) "444"
6) "456"
127.0.0.1:6379> spop myset
"456"
127.0.0.1:6379> spop myset
"444"
127.0.0.1:6379> spop myset
"123"
127.0.0.1:6379> spop myset
"222"
127.0.0.1:6379> smembers myset
1) "111"
2) "333"


===============移动set中给定值到另一个set中============
127.0.0.1:6379> smembers myset
1) "111"
2) "333"
127.0.0.1:6379> smove myset myset2 111
(integer) 1
127.0.0.1:6379> smembers myset
1) "333"
127.0.0.1:6379> smembers myset2
1) "111"

===============集合的交集、差集、并集=================
# sdiff 差集
# sinter 交集 (使用场景 B站中的共同关注)
# sunion 并集
127.0.0.1:6379> smembers myset2
1) "111"
2) "444"
127.0.0.1:6379> smembers myset
1) "2"
2) "111"
3) "333"
127.0.0.1:6379> sdiff myset myset2
1) "2"
2) "333"
127.0.0.1:6379> sinter myset myset2
1) "111"
127.0.0.1:6379> sunion myset myset2
1) "2"
2) "111"
3) "333"
4) "444"

应用场景:

微博,把各用户所有关注的人放在一个set集合中,粉丝也放在一个集合中!

就可以实现:

  • 共同关注
  • 共同爱好
  • 二度好友(六度分隔理论:你和任何一个陌生人之间所间隔的人不会超五个,也就是说,最多通过六个人你就能够认识任何一个陌生人。)

4. Redis-hash(哈希)

Map集合,key-map!这时候这个值是一个map集合!

本质和String没有太大区别,还是一个简单的key-value(value换成map)

==============存取hash==============
#hset hget
127.0.0.1:6379> hset myhash field Gangbb
(integer) 1
127.0.0.1:6379> hget myhash field
"Gangbb"

==============批量存取=====================
# hmset hmget hgetall
127.0.0.1:6379> hmset myhash field 123 field2 456
OK
127.0.0.1:6379> hmget myhash field field2
1) "123"
2) "456"
127.0.0.1:6379> hgetall myhash
1) "field"
2) "123"
3) "field2"
4) "456"


=============删除hash指定字段======================
# hdel
127.0.0.1:6379> hgetall myhash
1) "field"
2) "123"
3) "field2"
4) "456"
127.0.0.1:6379> hdel myhash field2
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "field"
2) "123"

=================求hash长度=======================
# hlen
127.0.0.1:6379> hgetall myhash
1) "field"
2) "123"
3) "key1"
4) "111"
5) "key2"
6) "222"
127.0.0.1:6379> hlen myhash
(integer) 3

================判断hash某个field是否存在============
# hexists
127.0.0.1:6379> hgetall myhash
1) "field"
2) "123"
3) "key1"
4) "111"
5) "key2"
6) "222"
127.0.0.1:6379> hexists myhash field
(integer) 1
127.0.0.1:6379> hexists myhash field2
(integer) 0

============获得hash中所有的field或value =============
# hkeys  hvals
127.0.0.1:6379> hgetall myhash
1) "field"
2) "123"
3) "key1"
4) "111"
5) "key2"
6) "222"
127.0.0.1:6379> hkeys myhash
1) "field"
2) "key1"
3) "key2"
127.0.0.1:6379> hvals myhash
1) "123"
2) "111"
3) "222"

================i++或i--操作============
# hincrby key field 步长
127.0.0.1:6379> hset myhash view 1
(integer) 1
127.0.0.1:6379> hget myhash view
"1"
127.0.0.1:6379> hincrby myhash view 1 #view+1
(integer) 2
127.0.0.1:6379> hget myhash view
"2"
127.0.0.1:6379> hincrby myhash view 3  #view+3
(integer) 5
127.0.0.1:6379> hget myhash view
"5"
127.0.0.1:6379> hincrby myhash view -2  #view-2
(integer) 3
127.0.0.1:6379> hget myhash view
"3"

================判断是否存在做set操作================
#hsetnx
127.0.0.1:6379> hgetall hash1
1) "f1"
2) "v1"
127.0.0.1:6379> hsetnx hash1 f2 v2 #没有则创建
(integer) 1
127.0.0.1:6379> hsetnx hash1 f2 v2 #有则不创建
(integer) 0
127.0.0.1:6379> hgetall hash1
1) "f1"
2) "v1"
3) "f2"
4) "v2"
127.0.0.1:6379> 


hash变更dd 数据user、name、age。尤其是用户信息之类的,经常变动的信息!hash更适合对象的存储,String更适合做字符串的存储。

5. Redis-Zset

有序集合

有序集合的编码可以是 ziplist 或者 skiplist

ziplist 编码的有序集合对象使用压缩列表作为底层实现, 每个集合元素使用两个紧挨在一起的压缩列表节点来保存, 第一个节点保存元素的成员(member), 而第二个元素则保存元素的分值(score)。

====================添加和获取值===========
# zadd zrange
127.0.0.1:6379> zadd price 8.5 apple #添加一个值
(integer) 1
127.0.0.1:6379> zadd price 9.0 banana 10 cherry  #添加多个值
(integer) 2
127.0.0.1:6379> zrange price 0 -1  #获取切范围中改动值
1) "apple"
2) "banana"
3) "cherry"

=================排序======================
# zrangebyscore key min max
# zrevrange
127.0.0.1:6379> zrangebyscore price -inf +inf  #从小到大排序所有  -inf负无穷 +inf正无穷
1) "apple"
2) "banana"
3) "cherry"
127.0.0.1:6379> zrangebyscore price -inf +inf withscores #从小到大排序所有 带上一些字段
1) "apple"
2) "8.5"
3) "banana"
4) "9"
5) "cherry"
6) "10"
127.0.0.1:6379> zrangebyscore price -inf 9 withscores #设置max值为9
1) "apple"
2) "8.5"
3) "banana"
4) "9"
127.0.0.1:6379> zrevrange price 0 -1 #从大到小排序
1) "cherry"
2) "banana"


==================移除元素=================
# zrem
127.0.0.1:6379> zrange price 0 -1
1) "apple"
2) "banana"
3) "cherry"
127.0.0.1:6379> zrem price apple
(integer) 1
127.0.0.1:6379> zrange price 0 -1
1) "banana"
2) "cherry"

===============获取集合中的个数===========
# zcard
127.0.0.1:6379> zrange price 0 -1
1) "banana"
2) "cherry"
127.0.0.1:6379> zcard price
(integer) 2


=============获取某个区间中的值个数=======
# zcount
127.0.0.1:6379> zrange price 0 -1 withscores
 1) "egg"
 2) "5"
 3) "something"
 4) "7"
 5) "banana"
 6) "9"
 7) "cherry"
 8) "10"
 9) "ball"
10) "100"
127.0.0.1:6379> zcount price 0 10
(integer) 4

案例思路:

set 排序 存储班级成绩表、工资表排序!

普通消息:1;重要消息:2. 可实现带权重判断

微博热搜榜、B站播放排行榜

你可能感兴趣的:(#,Redis,数据库,redis,数据库)