我有一把30秒后就过期的 keys .

redisTemplate.expire(sessionId , 30, TimeUnit.SECONDS);

这是我的听众:

@Service
public class RedisController implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // Get every expired key from below the code.
        String key = new String(message.getBody());
        System.out.println("expired key is: " + key);
    }
}

我也设置了CONFIG SET notify-keyspace-events KEA,但我没有收到任何回调.

推荐答案

虽然您的监听程序看起来不错,但我想您缺少监听程序(source)的配置.

@Bean
RedisMessageListenerContainer keyExpirationListenerContainer(RedisConnectionFactory connectionFactory, ExpirationListener expirationListener) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(expirationListener, new PatternTopic("__keyevent@*__:expired"));
    container.setErrorHandler(e -> logger.error("There was an error in Redis key expiration listener container", e));
    return container;
}

我想您可能也会感兴趣(我在浏览Java Doc API时发现了它们):

Java相关问答推荐

@org.springframework.beans.factory.annotation.Autowired(required=true)-注入点有以下注释:-SpringBoot

对运行在GraalVM-21上的JavaFX应用程序使用分代ZGC会警告不支持JVMCI,为什么?

存根基类的受保护方法

在Java中,在单个逻辑行中连接列表和单个元素的正确方法是什么?

GSON期间的Java类型擦除

MySQL数据库中未应用具有Spring数据的唯一约束

每次FXMLLoader调用ApplationConext.getBean(类)时创建@Component的新实例

迁移到Java 17后,日期显示不准确

如何创建模块信息类文件并将其添加到JAR中?

根本不显示JavaFX阿拉伯字母

如何生成指定范围内的11位序列号?

Java中HashSet的搜索时间与TreeSet的搜索时间

RestTemplate Bean提供OkHttp3ClientHttpRequestFactory不支持Spring Boot 3中的请求正文缓冲

如何使用jooq更新记录?

如何使用Java对随机生成的字母数字优惠券代码进行过期设置

将基于实例编号的对象列表拆分为新的对象列表

Intellij 2023 IDE:始终在顶部显示菜单栏

[Guice/MissingImplementation]:未绑定任何实现

为什么 log4j 过滤器在appender中不起作用

在 MapStruct 中,如何在 @Mapping 和 @Named 方法中使用相同的方法参数?