溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

spring boot+redis如何實(shí)現(xiàn)監(jiān)聽過期Key的方法

發(fā)布時(shí)間:2020-08-15 11:28:59 來源:億速云 閱讀:720 作者:小新 欄目:開發(fā)技術(shù)

spring boot+redis如何實(shí)現(xiàn)監(jiān)聽過期Key的方法?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

前言:

在訂單業(yè)務(wù)中,有時(shí)候需要對訂單設(shè)置有效期,有效期到了后如果還未支付,就需要修改訂單狀態(tài)。對于這種業(yè)務(wù)的實(shí)現(xiàn),有多種不同的辦法,比如:

1、使用querytz,每次生成一個(gè)訂單,就創(chuàng)建一個(gè)定時(shí)任務(wù),到期后執(zhí)行業(yè)務(wù)代碼;

2、rabbitMq中的延遲隊(duì)列;

3、對Redis的Key進(jìn)行監(jiān)控;

 1、引入依賴

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>

2、修改boot的redis配置

spring:
 #redis
 redis:
 database: 0
 host: 127.0.0.1
 password: redis_123456
 port: 6379

3、在服務(wù)器中 修改redis.conf配置文件(原來notify-keyspace-events 屬性是" " 空的,我們只需要填上“Ex”就行了)

notify-keyspace-events "Ex"

4、創(chuàng)建一個(gè)Redis監(jiān)控類,用于監(jiān)控過期的key,該類需繼承KeyExpirationEventMessageListener

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
 
import java.nio.charset.StandardCharsets;
 
/**
 * @program: SpringCloud
 * @description: redis Key過期監(jiān)聽
 * @author: zhang yi
 * @create: 2020-03-24 14:14
 */
public class KeyExpiredListener extends KeyExpirationEventMessageListener {
 
 public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
  super(listenerContainer);
 }
 
 @Override
 public void onMessage(Message message, byte[] pattern) {
  System.out.println("過期key:" + message.toString());
 }
}

5、創(chuàng)建Redis配置類

import com.zy.rabbitmq.base.Listener.KeyExpiredListener;
import org.springframework.beans.factory.annotation.Autowired;
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;
 
/**
 * @program: SpringCloud
 * @description: redis配置類
 * @author: zhang yi
 * @create: 2020-03-24 14:17
 */
@Configuration
public class RedisConfiguration {
 
 @Autowired
 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());
 }
 
}

6、這里提供一個(gè)redis工具類,用于存儲值,獲取值,獲取過期時(shí)間等操作。

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
 * redis 工具類
 * @Author ZhangYi
 */
@Component
public class RedisUtil {
 
	@Resource
	private RedisTemplate<String, Object> redisTemplate;
 
	/**
	 * 指定緩存失效時(shí)間
	 * 
	 * @param key 鍵
	 * @param time 時(shí)間(秒)
	 * @return
	 */
	public boolean expire(String key, long time) {
		try {
			if (time > 0) {
				redisTemplate.expire(key, time, TimeUnit.SECONDS);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
 
	/**
	 * 根據(jù)key 獲取過期時(shí)間
	 * 
	 * @param key 鍵 不能為null
	 * @return 時(shí)間(秒) 返回0代表為永久有效
	 */
	public long getExpire(String key) {
		return redisTemplate.getExpire(key, TimeUnit.SECONDS);
	}
 
	/**
	 * 判斷key是否存在
	 * 
	 * @param key 鍵
	 * @return true 存在 false不存在
	 */
	public boolean hasKey(String key) {
		try {
			return redisTemplate.hasKey(key);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
 
	/**
	 * 刪除緩存
	 * 
	 * @param key 可以傳一個(gè)值 或多個(gè)
	 */
	@SuppressWarnings("unchecked")
	public void del(String... key) {
		if (key != null && key.length > 0) {
			if (key.length == 1) {
				redisTemplate.delete(key[0]);
			} else {
				redisTemplate.delete(CollectionUtils.arrayToList(key));
			}
		}
	}
 
	// ============================String=============================
	/**
	 * 普通緩存獲取
	 * 
	 * @param key 鍵
	 * @return 值
	 */
	public Object get(String key) {
		return key == null &#63; null : redisTemplate.opsForValue().get(key);
	}
 
	/**
	 * 普通緩存放入
	 * 
	 * @param key 鍵
	 * @param value 值
	 * @return true成功 false失敗
	 */
	public boolean set(String key, Object value) {
		try {
			redisTemplate.opsForValue().set(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
 
	}
 
	/**
	 * 普通緩存放入并設(shè)置時(shí)間
	 * 
	 * @param key 鍵
	 * @param value 值
	 * @param time 時(shí)間(秒) time要大于0 如果time小于等于0 將設(shè)置無限期
	 * @return true成功 false 失敗
	 */
	public boolean set(String key, Object value, long time) {
		try {
			if (time > 0) {
				redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
			} else {
				set(key, value);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}

7、測試。(這里開放兩個(gè)接口,一個(gè)set值,并設(shè)置過期時(shí)間為10秒,一個(gè)獲取值和過期時(shí)間,當(dāng)?shù)竭_(dá)過期時(shí)間,看是否回去到過期Key)

@GetMapping("/put")
 public String demo(){
  redisUtil.set("name","zhangyi",10);
  return "aaa";
 }
 
 @GetMapping("/get")
 public Map<String,Object> get(){
  Map<String,Object> m =new HashMap<>();
  m.put("time",redisUtil.getExpire("name"));
  m.put("val",redisUtil.get("name"));
  return m;
 }

spring boot+redis如何實(shí)現(xiàn)監(jiān)聽過期Key的方法

spring boot+redis如何實(shí)現(xiàn)監(jiān)聽過期Key的方法

成功獲取到了過期Key,這里亂碼是因?yàn)閎oot集成的Redis存key或者value的時(shí)候,沒有配置字符串序列化。沒有配置的話是默認(rèn)使用jdk本身的序列化的。

感謝各位的閱讀!看完上述內(nèi)容,你們對spring boot+redis如何實(shí)現(xiàn)監(jiān)聽過期Key的方法大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI