Redis
reids 命令
# redis客户端参数
redis-cli -h
# 进入redis客户端
redis-cli -p 6380
- redis 中默认有16个库, 每个库之间都是相互隔离的
# 向1号库放入数据
set k1:1 hello
get k1:1
# 切换库
select 2
# redis客户端使用帮助
help <tab> # 命令补全
# 清空缓存
flushall
flushdb
# 获取value的类型
type k1
# 其中set命令只能存入string类型的数据
help set
SET key value [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL] [NX|XX] [GET]
summary: Set the string value of a key
since: 1.0.0
group: string
# 获取value的类型
type k1
string
String类型的value
- String 类型
help STRING
# 存入一个字符串
set k1 hello
# 当key不存在的时候, 设置value -> nx : 分布式锁时可以用
set k2 hello nx
# 当key存在的时候, 设置value -> xx : 只能做更新操作
set k1 world
# 多次存入
mset k3 hello k4 world
mget k3 k4
# 字符串追加
append k1 " world"
# 按字符串索引取字符穿 -> redis支持正向索引, 和负向索引 最后一位为-1, 倒数第二位为-3
getrange k1 2 4
# 字符串指定偏移量处覆盖字符
setrange k1 3 " setrange"
# 获取字符串长度
strlen k1
# 中文字符在redis中的长度 (由编码集的字节长度决定)
127.0.0.1:6380> set chinese 中
OK
127.0.0.1:6380> STRLEN chinese
(integer) 3
127.0.0.1:6380> append chinese 国
(integer) 6
127.0.0.1:6380> STRLEN chinese
(integer) 6
# 设置新值, 并返回旧值
127.0.0.1:6380> set k3 hello
OK
127.0.0.1:6380> GETSET k3 world
"hello"
127.0.0.1:6380> get k3
"world"
127.0.0.1:6380>
# msetnx 只有当其中的key不存在的时候, 同时存入多个key-value
127.0.0.1:6380> MSETNX k1 a k2 b
(integer) 1
127.0.0.1:6380> mget k1 k2
1) "a"
2) "b"
127.0.0.1:6380> MSETNX k2 c k3 c
(integer) 0
127.0.0.1:6380> mget k1 k2 k3
1) "a"
2) "b"
3) (nil)
127.0.0.1:6380>
- 数值类型
# 使用Object命令查看string类型的value的编码类型
127.0.0.1:6380> set k2 99
OK
127.0.0.1:6380> type k2
string
127.0.0.1:6380> OBJECT ENCODING k2
"int"
# 因为编码类型为int类型, 所以可以做一些数值操作
# 自增
127.0.0.1:6380> INCR k2
(integer) 100
# 自减
127.0.0.1:6380> DECR k2
(integer) 99
# 增加指定数值
127.0.0.1:6380> INCRBY k2 50
(integer) 149
# 减少指定数值
127.0.0.1:6380> DECRBY k2 11
(integer) 138
# 小数操作
127.0.0.1:6380> INCRBYFLOAT k2 0.001
"138.001"
- 二进制安全
在redis进程与外界交互的时候, redis从socket中取出的只有字节流, 没有字符流, 因为redis只存字节, 不是字节则按照编码集转换, 因此, 只要客户端存取的时候, 有统一的编解码, 数据就不会被破坏
# utf-8编码
127.0.0.1:6380> set k1 中
OK
# gbk编码
127.0.0.1:6380> set k2 中
OK
127.0.0.1:6380> get k1
"\xe4\xb8\xad"
127.0.0.1:6380> get k2
"\xd6\xd0"
# 格式化启动客户端, 可以正常显示中文
redis-cli -p 6380 --raw
# GBK显示
0.0.1:6380> get k2
中
127.0.0.1:6380> get k1
涓�
# utf-8显示
127.0.0.1:6380> get k1
中
127.0.0.1:6380> get k2
��
-
bitmap
在redis中存储字节时, 字节存在索引, 如图
127.0.0.1:6380> help SETBIT
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0
group: bitmap
127.0.0.1:6380> SETBIT k1 1 1
(integer) 0
127.0.0.1:6380> STRLEN k1
(integer) 1
127.0.0.1:6380> get k1
"@" # 01000000
127.0.0.1:6380> SETBIT k1 7 1
(integer) 0
127.0.0.1:6380> STRLEN k1
(integer) 1
127.0.0.1:6380> get k1
"A" # 01000001
127.0.0.1:6380> SETBIT k1 9 1
(integer) 0
127.0.0.1:6380> STRLEN k1
(integer) 2
127.0.0.1:6380> get k1
"A@" # 01000001 01000000
127.0.0.1:6380> SETBIT k1 17 0
(integer) 0
127.0.0.1:6380> STRLEN k1
(integer) 3
127.0.0.1:6380> get k1
"A@\x00" # 01000001 01000000 00000000
# 在所选择的索引中, 找到第一个目标bit
BITPOS key bit [start [end [BYTE|BIT]]]
summary: Find first bit set or clear in a string
since: 2.8.7
group: bitmap
127.0.0.1:6380> BITPOS k1 1 0 0 # k1-key, 1-查询的目标bit, 0-字节索引开始处, 0-字节索引截至处
(integer) 1 # 返回查询的目标bit索引(位索引)
127.0.0.1:6380> BITPOS k1 1 1 0
(integer) 9
# 统计在所选择的索引中, 1出现的次数
BITCOUNT key [start end [BYTE|BIT]]
summary: Count set bits in a string
since: 2.6.0
group: bitmap
127.0.0.1:6380> BITCOUNT k1 0 0 # k1-key, 0-字节索引开始处, 0-字节索引截至处
(integer) 2 # 在所查询的范围内, 1 出现了两次
# 位操作, 与 非 或 异或 and not or xor
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
group: bitmap
127.0.0.1:6380> SETBIT k1 1 1
(integer) 0
127.0.0.1:6380> SETBIT k1 7 1
(integer) 0
127.0.0.1:6380> get k1
"A" # 01000001
127.0.0.1:6380> SETBIT k2 1 1
(integer) 0
127.0.0.1:6380> SETBIT k2 6 1
(integer) 0
127.0.0.1:6380> get k2
"B" # 01000010
127.0.0.1:6380> BITOP and andkey k1 k2 # 将k1和k2进行位与操作(and), 并将结果存入 andkey中
(integer) 1
127.0.0.1:6380> get andkey
"@"
# 01000001 -k1
# 01000010 -k2
# 01000000 -andkey
-
bitmap应用场景
- 统计用户登陆天数: 可以记录哪天登陆, 且支持随机窗口查询登陆天数
setbit userId 1 1 setbit userId 2 1 setbit userId 365 1
- 统计某天的活跃用户数: 以日期为key, 用户id为位索引, 登陆过更新为1
setbit date1 userId 1 setbit date2 userId 1
如果统计某几天的活跃用户数:
bitop and activeNum date1 date2 date3 ...