1、redis 对事件的监听默认是关闭的,因为这会消耗性能
2、开启事件
修改redis.config 文件
############################# EVENT NOTIFICATION ##############################
# Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at http://redis.io/topics/notifications
#
# For instance if keyspace events notification is enabled, and a client
# performs a DEL operation on key "foo" stored in the Database 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to select the events that Redis will notify among a set
# of classes. Every class is identified by a single character:
#
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E Keyevent events, published with __keyevent@<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
notify-keyspace-events ""
3.有哪些事件可以监听呢?
- K 键空间通知,所有通知以 keyspace@ 为前缀,针对Key
- E 键事件通知,所有通知以 keyevent@ 为前缀,针对event
- g DEL 、 EXPIRE 、 RENAME 等类型无关的通用命令的通知
- $ 字符串命令的通知
- l 列表命令的通知
- s 集合命令的通知
- h 哈希命令的通知
- z 有序集合命令的通知
- x 过期事件:每当有过期键被删除时发送
- e 驱逐(evict)事件:每当有键因为 maxmemory 政策而被删除时发送
- A 参数 g$lshzxe 的别名,相当于是All
输入的参数中至少要有一个 K 或者 E , 否则的话, 不管其余的参数是什么, 都不会有任何通知被分发。上表中斜体的部分为通用的操作或者事件,而黑体则表示特定数据类型的操作。例如,“Kx”表示想监控某个Key的失效事件。
4.下面我们开始在springboot上来实现监听,直接上代码
**import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* 配置redis监听事件
* Created by lpgu on 2019/8/1.
*/
@Configuration
public class RedisConfiguration {
@Autowired
RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(){
RedisMessageListenerContainer redisMessageListenerContainer=new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
return redisMessageListenerContainer;
}
@Bean
public KeyExpirationEventMessageListener keyExpirationEventMessageListener(){
return new KeyExpirationEventMessageListener(this.redisMessageListenerContainer());
}
}**
import com.jdrx.iot.beans.entry.CommandRecordPO;
import com.jdrx.iot.dao.CommandRecordDao;
import com.jdrx.iot.enums.EnumCommand;
import com.jdrx.iot.redis.dao.DeviceRedisDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import 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;
import java.nio.charset.StandardCharsets;
/**
* redis的key过期监听
* Created by lpgu on 2019/8/1.
*/
@Component
public class KeyExpiredListener extends KeyExpirationEventMessageListener {
private final static Logger logger = LoggerFactory.getLogger(KeyExpiredListener.class);
public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
//key=iot:device:command:868474043308814_b39d7fb91ef74ff6a205761b62cff0fb_aaaaaa
String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
//过期的key
String key = new String(message.getBody(), StandardCharsets.UTF_8);
logger.info("redis key 过期:pattern={},channel={},key={}", new String(pattern), channel, key);
}
}