引入依赖
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;
@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;
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;
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";
private static JedisPool getRedisLink() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100); jedisPoolConfig.setMaxIdle(10); return new JedisPool(jedisPoolConfig, HOST, PORT); }
private static void closeRedis(JedisPool jedisPool, Jedis jedis) { jedis.close(); jedisPool.close(); }
private static Jedis getJedis(JedisPool jedisPool) { Jedis jedis = jedisPool.getResource(); jedis.auth(AUTH_PASSWORD); return jedis; }
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); }
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秒的过期时间