redis使用基础
redis springboot常用命令
1 |
|
reids设置过期key监听事件
在redis命令行执行
config set notify-keyspace-events Ex
在java代码添加监听事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
public void onMessage(Message message, byte[] pattern) {
// 处理接收到的消息
String channel = new String(message.getChannel());
String body = new String(message.getBody());
System.out.println("接收到来自通道 " + channel + " 的消息:" + body);
}
}等待过期,就会打印如下内容
1
2
3接收到来自通道 __keyevent@1__:expired 的消息:GATE:DEVICE:HEART:1725361717475983361
接收到来自通道 __keyevent@1__:expired 的消息:GATE:DEVICE:HEART:1728969231125295105
接收到来自通道 __keyevent@1__:expired 的消息:GATE:DEVICE:HEART:1726505401086349313
从redis里面获取二进制图片
redis的key是camera:frames:1838502535022682114
,类型是SORTED SET
,里面的数据格式是:
Member | Score |
---|---|
\xff\xd8\xff\xe0\x00\x10JFIF\x…..\t\x8e\xc7\xff\xd9 | 173312719710 |
\xff\xd8\xff\xe0\x00\x10JFIF\x…..\t\x8e\xc7\xff\xd9 | 173312719711 |
\xff\xd8\xff\xe0\x00\x10JFIF\x…..\t\x8e\xc7\xff\xd9 | 173312719712 |
对应的java代码:
1 |
|
对应的序列号类:ByteArrayRedisSerializer
1 | import org.springframework.data.redis.serializer.RedisSerializer; |
访问测试:apifox掉[http://127.0.0.1:8080/test/getVideoImg?deviceId=1838502535022682114]接口,就可以得倒base64的图片了,在apifox的后置脚本添加如下自定义脚本:
1 | // 获取图片数据 |
然后可以直接在结果切到Visualize
视图就可以看到图片了。
python里面设置可以java序列号的json字符串
最重要的是要在json外层添加"@class": "java.lang.Object"
,为什么是java.lang.Object
,因为这个支持嵌套,其他hash map
不支持多层嵌套,除非python在每层都是在@class
1 | import redis |
注意
reids重新设值会覆盖expireAt
过期时间的设置