引入依赖

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

application.yml配置Redis

1
2
3
4
5
6
spring:
redis:
database: 0
host: 121.41.222.106
password: 667866
port: 6379

Redis监听配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.ledao.config;

import com.ledao.listener.KeyExpiredListener;
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;

import javax.annotation.Resource;

/**
* Redis监听过期的key
*
* @author LeDao
* @company
* @create 2022-03-02 2:04
*/
@Configuration
public class RedisConfig {

@Resource
private RedisConnectionFactory redisConnectionFactory;

@Bean
public RedisMessageListenerContainer redisMessageListenerContainer() {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
return redisMessageListenerContainer;
}

@Bean
public KeyExpiredListener keyExpiredListener() {
return new KeyExpiredListener(this.redisMessageListenerContainer());
}
}

过期key监听类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.ledao.listener;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import java.util.Date;

/**
* 过期key监听类
*
* @author LeDao
* @company
* @create 2022-03-02 2:09
*/
public class KeyExpiredListener extends KeyExpirationEventMessageListener {

public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}

@Override
public void onMessage(Message message, byte[] pattern) {
System.out.println(new Date() + ": 过期的key为" + message.toString());
}
}

Jedis工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.ledao.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;

/**
* Redis工具类
*
* @author LeDao
* @company
* @create 2022-01-17 1:21
*/
public class RedisUtil {

private static final String HOST = "121.41.222.106";
private static final Integer PORT = 6379;
private static final String AUTH_PASSWORD = "667866";

/**
* 获取Redis连接
*
* @return
*/
private static JedisPool getRedisLink() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxIdle(10);
return new JedisPool(jedisPoolConfig, HOST, PORT);
}

/**
* 关闭Redis连接
*
* @param jedisPool
* @param jedis
*/
private static void closeRedis(JedisPool jedisPool, Jedis jedis) {
jedis.close();
jedisPool.close();
}

/**
* 获取Jedis
*
* @param jedisPool
* @return
*/
private static Jedis getJedis(JedisPool jedisPool) {
Jedis jedis = jedisPool.getResource();
jedis.auth(AUTH_PASSWORD);
return jedis;
}

/**
* 设置key
*
* @param key
* @param value
* @return
*/
public static boolean setKey(String key, String value) {
JedisPool jedisPool = getRedisLink();
Jedis jedis = getJedis(jedisPool);
String result = jedis.set(key, value);
closeRedis(jedisPool, jedis);
return "OK".equals(result);
}

/**
* 给指定key设置过期时间(秒)
*
* @param key
* @param seconds
*/
public static void setKeyTime(String key, int seconds) {
JedisPool jedisPool = getRedisLink();
Jedis jedis = getJedis(jedisPool);
jedis.expire(key, seconds);
closeRedis(jedisPool,jedis);
}
}

请求的方法

1
2
3
4
5
6
7
8
@ResponseBody
@RequestMapping("/setRedisKey")
public String setRedisKey(){
RedisUtil.setKey("a", "1");
RedisUtil.setKeyTime("a", 10);
System.out.println(new Date()+": "+"设置了key,过期时间为"+10+"秒");
return "设置成功";
}

测试

浏览器通过请求http://localhost/setRedisKey链接创建一个名为a的key,并给它设置10秒的过期时间

2022-03-02 025638