zoukankan      html  css  js  c++  java
  • redis可以设置过期key回调实现延时队列

          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <version>1.5.10.RELEASE</version>
            </dependency>
    

      

    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;
    
    @Component
    public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
        public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
            super(listenerContainer);
        }
    
        /**
         * 针对redis数据失效事件,进行数据处理
         * @param message
         * @param pattern
         */
        @Override
        public void onMessage(Message message, byte[] pattern) {
            // 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
            String expiredKey = message.toString();
            System.out.println(expiredKey);
        }
    }
    

      

    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.RedisMessageListenerContainer;
    
    @Configuration
    public class RedisListenerConfig {
    
        @Bean
        RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
    
            RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            container.setConnectionFactory(connectionFactory);
            return container;
        }
    }
    

      启动工程.

    redis.windows-service.conf 文件需要修改

    然后在此基础上把notify-keyspace-events Ex 这一行的注释打开

     set a 1 ex 2(2秒后过期)

    页面就会打印过期的key为a

  • 相关阅读:
    Falcon
    资源
    资源
    Python的高级Git库 Gittle_python_脚本之家
    How to provide username and password when run "git clone git@remote.git"?
    Python项目自动化部署最佳实践@搜狐 | the5fire的技术博客
    Jenkins+Maven+Git搭建持续集成和自动化部署的配置手记
    Scaffold a Flask Project
    git python
    CentOS 删除自带的OpenJDK 和 安装SunJDK
  • 原文地址:https://www.cnblogs.com/q1359720840/p/15319539.html
Copyright © 2011-2022 走看看