简介:
在redis的使用场景中,有时我们会对redis的键值进行监听,若redis的key过期,需要触发某些事件。
举例:
比如:用户下单成功,若三十分钟未付款,需要做某些事情。
使用前提:
redis版本 2.8.0+
修改redis.conf中的notify-keyspace-events Ex,默认为notify-keyspace-events ""
也可以使用执行命令:CONFIG set notify-keyspace-events Ex (采用此种方法若重启redis需重新设置)
代码实现:
配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:redis="http://www.springframework.org/schema/redis" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd"> 7 8 <context:component-scan base-package="com.abc.redis" /> 9 10 <!-- 开启注解 --> 11 <context:annotation-config /> 12 13 <!-- scanner redis properties --> 14 <context:property-placeholder location="classpath:redis.properties" file-encoding="utf-8" ignore-unresolvable="true"/> 15 16 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 17 <property name="maxIdle" value="1000" /> 18 <property name="maxTotal" value="10000" /> 19 <property name="testOnBorrow" value="true" /> 20 </bean> 21 22 <bean id="redisConnectionFactory" 23 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 24 <property name="usePool" value="true"></property> 25 <property name="hostName" value="127.0.0.1" /> 26 <property name="port" value="6379" /> 27 <!-- <property name="password" value="" />--> 28 <property name="timeout" value="10000" /> 29 <property name="database" value="1"></property> 30 <constructor-arg index="0" ref="jedisPoolConfig" /> 31 </bean> 32 33 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 34 <property name="connectionFactory" ref="redisConnectionFactory" /> 35 <property name="keySerializer" ref="stringRedisSerializer" /> 36 <property name="valueSerializer" ref="stringRedisSerializer" /> 37 <property name="hashKeySerializer" ref="stringRedisSerializer" /> 38 <property name="hashValueSerializer" ref="stringRedisSerializer" /> 39 </bean> 40 41 <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 42 <property name="connectionFactory" ref="redisConnectionFactory" /> 43 <property name="keySerializer" ref="stringRedisSerializer" /> 44 <property name="valueSerializer" ref="stringRedisSerializer" /> 45 <property name="hashKeySerializer" ref="stringRedisSerializer" /> 46 <property name="hashValueSerializer" ref="stringRedisSerializer" /> 47 </bean> 48 49 <bean id="stringRedisSerializer" 50 class="org.springframework.data.redis.serializer.StringRedisSerializer" > 51 </bean> 52 53 <redis:listener-container> 54 <redis:listener ref="listener" method="onMessage" topic="__keyevent@*__:expired"/> //监听的失效的key 55 <redis:listener ref="listener2" method="onMessage" topic="www"/> 56 </redis:listener-container> 57 <bean id="listener" class="com.abc.redis.RedisMessageListener" /> 58 <bean id="listener2" class="com.abc.redis.RedisMessageListener2" /> 59 </beans>
监听类:针对于失效的key
1 import org.springframework.data.redis.connection.Message; 2 import org.springframework.data.redis.connection.MessageListener; 3 import org.springframework.data.redis.core.RedisTemplate; 4 import org.springframework.stereotype.Service; 5 6 import javax.annotation.Resource; 7 8 /** 9 * @Author 10 * @Date 11 */ 12 @Service 13 public class RedisMessageListener implements MessageListener { 14 15 16 @Resource 17 private RedisTemplate redisTemplate; 18 19 public RedisTemplate getRedisTemplate() { 20 return redisTemplate; 21 } 22 23 public void setRedisTemplate(RedisTemplate redisTemplate) { 24 this.redisTemplate = redisTemplate; 25 } 26 27 public void onMessage(Message message, byte[] pattern) { 28 System.out.println(new String(message.getChannel())); 29 } 30 }
APP:
1 public class App { 2 3 public static void main(String[] args) { 4 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:redis-context.xml"); 5 6 System.out.println(context); 8 9 } 10 }
登录客户端命令:
127.0.0.1:6379> set mykey abcde
OK
127.0.0.1:6379> expire mykey 5
(integer) 1
127.0.0.1:6379> ttl mykey
(integer) -2
127.0.0.1:6379>