Redisキャッシュの期限切れの監視

Redisキャッシュの有効期限は、Redisサブスクリプションおよび公開機能(pub / sub)を介して配布されるため、redisイベントの監視と公開を有効にする必要があり
ます。redis構成ファイルを
変更します。notify - keyspace -events ""をnotify-keyspace-events "Ex"に変更して
再起動します。 Redis
は2つのクライアントを開いて、構成が有効かどうかをテストします。
クライアント1は有効期限を監視します。

SUBSCRIBE __keyevent@0__:expired

ここに写真の説明を挿入
クライアント2は、有効期限が1秒のキーを設定します。

set ex_key ex_value ex 1  

ここに写真の説明を挿入
クライアント1は期限切れのイベントを正常に監視しました
ここに写真の説明を挿入

javaモニター

pomファイル

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sunyuqi</groupId>
    <artifactId>redis_demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> 
    </parent>
    <properties>
        <java.version>9</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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

構成クラス

package com.sunyuqi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Profile("pubsub")
class RedisTemplateConfig {
    
    

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
    
    
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379));
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    
    
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 可以配置对象的转换规则,比如使用json格式对object进行存储。
        // Object --> 序列化 --> 二进制流 --> redis-server存储
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        return redisTemplate;
    }
}

期限切れのメッセージリスナークラスをキャッシュする

package com.sunyuqi.pubsub;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Profile("pubsub")
@Configuration
public class ExListener {
    
    
    // 定义监听器
    @Bean
    public RedisMessageListenerContainer smsMessageListener(RedisConnectionFactory redisConnectionFactory) {
    
    
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        SmsSendListener smsSendListener = new SmsSendListener();
        container.addMessageListener(smsSendListener, Arrays.asList(new ChannelTopic("__keyevent@0__:expired")));
        return container;
    }

    class SmsSendListener implements MessageListener {
    
    
        @Override
        public void onMessage(Message message, byte[] pattern) {
    
    
            System.out.println("key: " + message + "已过期。");
        }
    }
}

テストクラス

package com.sunyuqi.pubsub;

import com.sunyuqi.SpringbootApplicationDemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootApplicationDemo.class)
@ActiveProfiles("pubsub")
public class PubsubTest {
    
    

    @Autowired
    RedisTemplate redisTemplate;

    @Test
    public void test() throws InterruptedException {
    
    
        //设置一秒钟过期
        redisTemplate.opsForValue().set("ex_test","test",1,TimeUnit.SECONDS);
        Thread.sleep(2000);
    }
}

キャッシュの有効期限を正常に監視
ここに写真の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_42494845/article/details/108897410