欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Redis拓展之定時(shí)消息通知實(shí)現(xiàn)詳解

 更新時(shí)間:2023年07月05日 09:53:23   作者:右耳菌  
這篇文章主要為大家介紹了Redis拓展之定時(shí)消息通知實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1. Redis實(shí)現(xiàn)定時(shí)消息通知

簡單定時(shí)任務(wù)通知: 利用redis的keyspace notifications(即:鍵過期后事件通知機(jī)制)

開啟方法

  • 修改server.conf文件,找到notify-keyspace-events , 修改為“Ex”
  • 使用cli命令: redis-cli config set notify-keyspace-events Ex
  • redis 配置參考

2. 例子

創(chuàng)建springboot項(xiàng)目

修改pom.xml 和 yml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.9</version> <!-- 這個(gè)版本其實(shí)還是挺重要的,如果是2.7.3版本的話,大概會(huì)無法成功自動(dòng)裝載 RedisConnectionFactory -->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.lazyfennec</groupId>
    <artifactId>redisdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redisdemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

spring:
  redis:
    database: 0
    host: 192.168.1.7
    port: 6379
    jedis:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 1

創(chuàng)建RedisConfig

package cn.lazyfennec.redisdemo.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/9/20 23:19
 */
@Configuration
@EnableCaching
public class RedisConfig {
    @Bean
    public RedisTemplate&lt;String, Object&gt; redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 設(shè)置序列化
        Jackson2JsonRedisSerializer&lt;Object&gt; jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer&lt;Object&gt;(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 配置redisTemplate
        RedisTemplate&lt;String, Object&gt; redisTemplate = new RedisTemplate&lt;&gt;();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer); // key 序列化
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // value 序列化
        redisTemplate.setHashKeySerializer(stringSerializer); // Hash key 序列化
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // Hash value 序列化
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }  
}

創(chuàng)建RedisListenerConfiguration

package cn.lazyfennec.redisdemo.config;
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.RedisMessageListenerContainer;
/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/9/21 11:02
 */
@Configuration
public class RedisListenerConfiguration {
    @Autowired
    private RedisConnectionFactory factory;
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(factory);
        return redisMessageListenerContainer;
    }
}

事件監(jiān)聽事件 RedisTask

package cn.lazyfennec.redisdemo.task;
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;
/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/9/21 11:05
 */
@Component
public class RedisTask extends KeyExpirationEventMessageListener {
    public RedisTask(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 接收到事件后回調(diào)
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        String key = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("key:" + key + ", channel:" + channel);
    }
}

發(fā)布 RedisPublisher

package cn.lazyfennec.redisdemo.publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/9/21 16:34
 */
@Component
public class RedisPublisher {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    // 發(fā)布
    public void publish(String key) {
        redisTemplate.opsForValue().set(key, new Random().nextInt(200), 10, TimeUnit.SECONDS);
    }
    // 循環(huán)指定時(shí)間觸發(fā)
    @Scheduled(cron = "0/15 * * * * ?")
    public void scheduledPublish() {
        System.out.println("scheduledPublish");
        redisTemplate.opsForValue().set("str1", new Random().nextInt(200), 10, TimeUnit.SECONDS);
    }
}
  • 要實(shí)現(xiàn)Scheduled需要在啟動(dòng)類上加上注解
@SpringBootApplication
@EnableScheduling // 要加上這個(gè),用以啟動(dòng)
public class RedisdemoApplication {

修改TestController

package cn.lazyfennec.redisdemo.controller;
import cn.lazyfennec.redisdemo.publisher.RedisPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/9/21 16:32
 */
@RestController
public class TestController {
    @Autowired
    RedisPublisher redisPublisher;
    @GetMapping("/redis/{key}")
    public String publishEvent(@PathVariable String key) {
        // 發(fā)布事件
        redisPublisher.publish(key);
        return "OK";
    }
}

以上就是Redis拓展之定時(shí)消息通知實(shí)現(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于Redis定時(shí)消息通知的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 在Mac OS上安裝Vagrant和Docker的教程

    在Mac OS上安裝Vagrant和Docker的教程

    這篇文章主要介紹了在Mac OS上安裝Vagrant和Docker的教程,并安裝和設(shè)置Postgres和Elasticsearch和Redis,需要的朋友可以參考下
    2015-04-04
  • 使用Redis實(shí)現(xiàn)令牌桶算法原理解析

    使用Redis實(shí)現(xiàn)令牌桶算法原理解析

    這篇文章主要介紹了使用Redis實(shí)現(xiàn)令牌桶算法,該算法可以應(yīng)對(duì)短暫的突發(fā)流量,這對(duì)于現(xiàn)實(shí)環(huán)境中流量不怎么均勻的情況特別有用,不會(huì)頻繁的觸發(fā)限流,對(duì)調(diào)用方比較友好,需要的朋友可以參考下
    2021-12-12
  • Redis應(yīng)用之簽到的使用

    Redis應(yīng)用之簽到的使用

    在很多時(shí)候,我們遇到用戶簽到的場景,本文主要介紹了Redis應(yīng)用之簽到的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Redis的9種數(shù)據(jù)類型用法解讀

    Redis的9種數(shù)據(jù)類型用法解讀

    這篇文章主要介紹了Redis的9種數(shù)據(jù)類型用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 深入探究RedisJSON模塊的工作原理以及使用操作

    深入探究RedisJSON模塊的工作原理以及使用操作

    Redis推出了RedisJSON模塊,它允許開發(fā)者在Redis數(shù)據(jù)庫中直接存儲(chǔ)、查詢和處理JSON數(shù)據(jù),本文將詳細(xì)介紹RedisJSON的工作原理、關(guān)鍵操作、性能優(yōu)勢以及使用場景,需要的朋友可以參考下
    2024-05-05
  • SpringBoot集成Redis的思路詳解

    SpringBoot集成Redis的思路詳解

    Redis是一個(gè)開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。接下來通過本文給大家分享SpringBoot集成Redis的詳細(xì)過程,感興趣的朋友一起看看吧
    2021-10-10
  • Redis Cluster集群主從切換的踩坑與填坑

    Redis Cluster集群主從切換的踩坑與填坑

    這篇文章主要介紹了Redis Cluster集群主從切換的踩坑與填坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 解決Redis連接無法正常釋放的問題

    解決Redis連接無法正常釋放的問題

    這篇文章主要介紹了解決Redis連接無法正常釋放的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Redis實(shí)現(xiàn)多人多聊天室功能

    Redis實(shí)現(xiàn)多人多聊天室功能

    這篇文章主要為大家詳細(xì)介紹了Redis實(shí)現(xiàn)多人多聊天室功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • redis.conf中使用requirepass不生效的原因及解決方法

    redis.conf中使用requirepass不生效的原因及解決方法

    本文主要介紹了如何啟用requirepass,以及啟用requirepass為什么不會(huì)生效,從代碼層面分析了不生效的原因,以及解決方法,需要的朋友可以參考下
    2023-07-07

最新評(píng)論