【问题标题】:How to Enable Keyspace Notifications for Expired Keys using Spring Data Redis如何使用 Spring Data Redis 为过期键启用键空间通知
【发布时间】:2021-09-30 08:51:42
【问题描述】:

有没有办法从 Spring Data Redis 运行以下命令,可能使用RedisTemplate

$ redis-cli config set notify-keyspace-events Ex

我的理解是RedisTemplate可以运行lua脚本,上面可以转换成一个吗?

【问题讨论】:

    标签: spring-boot redis spring-data spring-data-redis


    【解决方案1】:

    回答我自己的问题,原来不需要运行 lua 脚本:

    如果使用非反应式 Redis 连接:

    RedisConnection conn = null;
    try {
        conn = connectionFactory.getConnection();
        conn.setConfig("notify-keyspace-events", "Ex");
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
    

    如果使用反应式 Redis 连接:

    ReactiveRedisConnection conn = connectionFactory.getReactiveConnection();
            conn
                    .serverCommands()
                    .setConfig("notify-keyspace-events", "Ex")
                    .filter(status -> status.equals("OK"))
                    .doFinally(unused -> conn.close())
                    .block(Duration.ofSeconds(5L));
    

    【讨论】:

      猜你喜欢
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多