1. 程式人生 > 其它 >SpringBoot + Redis 實現鍵空間通知(keyspace notification)

SpringBoot + Redis 實現鍵空間通知(keyspace notification)

目錄

前言

SpringBoot + Redis 可以用 Redis 的鍵空間通知機制實現類似延遲訊息佇列的功能 ,Redis2.8 後可以通過鍵空間通知接收那些以某種方式改變了Redis資料空間的事件通知,關於 Redis 鍵空間通知的配置 Redis-x64-3.2 鍵空間通知(keyspace notification) 之前有介紹,這裡只是介紹 SpringBoot 中的同理實現。


環境

SpringBoot2.5.3 + Redis-x64-3.2.1


具體實現

  • 啟動 redis,配置檔案 redis.windows.conf 中設定鍵空間通知事件為Ex
notify-keyspace-events Ex
  • application.yml
redis:
    localhost: localhost
    port: 6379 
    database: 7
    password:
    # 過期事件訂閱,接收7號資料庫中所有key的過期事件
    listen-pattern: __keyevent@7__:expired
  • Redis 事件廣播配置類
import com.coisini.springbootlearn.core.listener.RedisMessageListener;
import org.springframework.beans.factory.annotation.Value;
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.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

@Configuration
public class RedisListenerConfiguration {

    @Value("${spring.redis.listen-pattern}")
    public String pattern;

    @Bean
    public RedisMessageListenerContainer listenerContainer(RedisConnectionFactory redisConnection) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnection);

        /**
         * Topic是訊息釋出(Pub)者和訂閱(Sub)者之間的傳輸中介
         */
        Topic topic = new PatternTopic(this.pattern);

        container.addMessageListener(new RedisMessageListener(), topic);
        return container;
    }
}
  • Redis 事件廣播監聽器
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

public class RedisMessageListener implements MessageListener {

    /**
     * Redis 事件監聽回撥
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] body = message.getBody();

        String expiredKey = new String(body);

        System.out.println("監聽到已過期的key:" + expiredKey);

        /**
         * 監聽到過期事件回撥
         * TODO:
         */

    }
}
  • 測試介面
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping(value = "/setExpiredVal")
    public String setExpiredVal(@RequestParam String name) {
    	// 設定 20s 後過期
        redisTemplate.opsForValue().set("name", name, 20, TimeUnit.SECONDS);
        return "setVal is ok";
    }

}
  • 訪問介面
  • 20s後控制檯輸出如下:
  • 接下來就可以去處理相應的業務了。

- End -
夢想是鹹魚
關注一下吧
以上為本篇文章的主要內容,希望大家多提意見,如果喜歡記得點個推薦哦 作者:Maggieq8324 出處:https://www.cnblogs.com/maggieq8324/ 本文版權歸作者和部落格園共有,歡迎轉載,轉載時保留原作者和文章地址即可。