Spring boot Redis 失效监听 记录

超时监听: 开启,键事件通知(key-event notification),在key失效时,会发布事件;

开启配置:
Spring boot Redis 失效监听 记录_第1张图片

注意: 因为你 reids 失效的key 有很多,最好把需要监听的 key 放在指定的 db里,监听那个db,最业务处理。
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/**
 * @program: security
 * @description: 配置
 * @author: he
 * @create: 2019-11-28 09:07
 **/
public class RedisExpiredListener implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] bytes) {
        byte[] body = message.getBody();// 建议使用: valueSerializer
        byte[] channel = message.getChannel();
        System.out.print("reids出行订单失效时间开始... " );
        System.out.println(String.format("channel: %s, body: %s, bytes: %s"
                ,new String(channel), new String(body), new String(bytes)));

    }

}
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.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @program: security
 * @description: redis 消息监听配置
 * @author: he
 * @create: 2019-11-28 09:02
 *
 * 订阅 __keyevent@0__:expired 通道   0: db0
 *
 **/
@Configuration
public class RedisListenerConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(new RedisExpiredListener(), new PatternTopic("__keyevent@5__:expired"));
        return container;
    }


}
import com.ld.common.db.QueryConditions;
import com.ld.common.expcetion.BaseException;
import com.ld.model.DailyJourney;
import com.ld.model.DriverQuotedPrice;
import com.ld.service.DailyJourneyService;
import com.ld.service.DriverQuotedPriceService;
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 javax.annotation.Resource;
import java.util.regex.Pattern;

/**
 * @program: security
 * @description: redis失效key监听
 * @author: he
 * @create: 2019-11-28 09:05
 **/
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Resource
    private DailyJourneyService dailyJourneyService;
    @Resource
    private DriverQuotedPriceService driverQuotedPriceService;


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

    /**
     * redis失效key事件处理, 监听第5个分片。
     * @param message  失效key
     * @param pattern  监听匹配 eg: __keyevent@5__:expired
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
        if (isNumeric(expiredKey)){
            try {
                Long dailyJourneyId = Long.parseLong(expiredKey);//出行id
                DailyJourney dailyJourney = dailyJourneyService.searchById(dailyJourneyId);
                if (dailyJourney!=null){
                    DriverQuotedPrice driverQuotedPrice = driverQuotedPriceService.searchOneByConditions(new QueryConditions().where().e("daily_journey_id", dailyJourneyId).get());
                    //如果出行订单,在失效时间时没有司机接单
                    if (driverQuotedPrice==null && dailyJourney.getStatus().equals("0")){
                        DailyJourney dailyJourney1 = new DailyJourney();
                        dailyJourney1.setDailyJourneyId(dailyJourneyId);
                        dailyJourney1.setStatus("9");
                        dailyJourneyService.modify(dailyJourney1);
                    }
                }
            } catch (BaseException e) {
                e.printStackTrace();
            }
        }
    }

    private static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}

你可能感兴趣的:(缓存)