亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java操作redis設(shè)置第二天凌晨過期的解決方案

 更新時間:2022年01月07日 09:49:43   作者:shyの程序猿  
這篇文章主要介紹了Java操作redis設(shè)置第二天凌晨過期的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Java操作redis設(shè)置第二天凌晨過期

場景

在做查詢數(shù)據(jù)的時候,遇到了需要設(shè)置數(shù)據(jù)在redis中第二天過期的問題,但是redis又沒有對應(yīng)的API,就只好自己來解決了

思路

計算出第二天凌晨與當前時間的時間差,將該時間差設(shè)置為redis的過期時間,就可以達到我們想要的效果

代碼

/**
? ? ?* 計算第二天凌晨與當前時間的時間差秒數(shù)
? ? ?* @param
? ? ?* @return java.lang.Long
? ? ?* @author shy
? ? ?* @date 2021/3/12 18:10
? ? ?*/
? ? public static Long getNowToNextDaySeconds() {
? ? ? ? Calendar cal = Calendar.getInstance();
? ? ? ? cal.add(Calendar.DAY_OF_YEAR, 1);
? ? ? ? cal.set(Calendar.HOUR_OF_DAY, 0);
? ? ? ? cal.set(Calendar.SECOND, 0);
? ? ? ? cal.set(Calendar.MINUTE, 0);
? ? ? ? cal.set(Calendar.MILLISECOND, 0);
? ? ? ? return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
? ? }

拿到了時間差,剩下的基本上就沒什么問題了。

附上Redis工具類:

/**
 * 操作redis
 * @author shy
 * @date 2020/12/10 10:01
 */
@Service
public class RedisService {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
    private RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 判斷String類型key是否存在
	 *
	 * @param key
	 * @return 
	 * @author shy
	 * @date 2018年11月13日 下午1:40:37
	 */
	public boolean hasStringKey(String key) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		return stringRedisTemplate.opsForValue().getOperations().hasKey(key);
	}
	
	/**
	 * 判斷String類型key是否存在
	 *
	 * @param key
	 * @return 
	 * @author shy
	 * @date 2018年11月13日 下午1:43:51
	 */
	public boolean nonStringKey(String key) {
		return !hasStringKey(key);
	}
	/**
	 * 設(shè)置String類型key,String類型value,過期時間timeout,TimeUnit
	 *
	 * @param key
	 * @param value
	 * @param timeout
	 * @param timeUnit
	 * @author shy
	 * @date 2018年12月10日13:53:38
	 */
	public void setStringKey(String key, String value, Long timeout, TimeUnit timeUnit) {
		if (StringUtils.isBlank(key) || Objects.isNull(timeout)) {
			throw new EmptyParameterException();
		}
		stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);
	}
	
	public void setStringKey(String key, String value) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		stringRedisTemplate.opsForValue().set(key, value);
	}
	/**
	 * 獲取String類型value
	 *
	 * @param key
	 * @return
	 * @author shy
	 * @date 2018年11月12日 下午7:09:31
	 */
	public String getStringValue(String key) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		return stringRedisTemplate.opsForValue().get(key);
	}
	
	/**
	 *	獲取Key的過期時間
	 *
	 * @param key
	 * @return
	 * @author shy
	 * @date 2019年4月25日17:28:36
	 */
	public Long getExpire(String key) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		return stringRedisTemplate.getExpire(key);
	}
	
	/**
	 *	設(shè)置Key的過期時間
	 *
	 * @param key
	 * @return
	 * @author shy
	 * @date 2019年4月25日17:28:36
	 */
	public Boolean setExpire(String key,Long timeout, TimeUnit timeUnit) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		return stringRedisTemplate.expire(key, timeout, timeUnit);
	}	
	
	/**
	 * value自增+n
	 * @param key
	 * @return
	 * @author shy
	 * @date 2019年4月8日15:54:30
	 */
	public Long setIncrementValue(String key) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		return stringRedisTemplate.opsForValue().increment(key, 1L);
	}
	/**
	 * 設(shè)置String類型key,Object類型value,過期時間timeout
	 *
	 * @param key
	 * @param value
	 * @param timeout
	 * @author shy
	 * @date 2018年12月10日13:54:07
	 */
	public void setObjectKey(String key, Object value, Long timeout,TimeUnit time) {
		if (StringUtils.isBlank(key) || Objects.isNull(timeout)) {
			throw new EmptyParameterException();
		}
		redisTemplate.opsForValue().set(key, value, timeout, time);
	}
	
	public void setObjectKey(String key, Object value) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		redisTemplate.opsForValue().set(key, value);
	}
	
	/**
	 * 獲取Object類型value
	 *
	 * @param key
	 * @param clazz
	 * @return 
	 * @author shy
	 * @date 2019年11月6日10:01:30
	 */
	@SuppressWarnings("unchecked")
	public <T> T getObjectValue(String key, Class<T> clazz) {
		if (StringUtils.isBlank(key)) {
			return null;
		}
		return (T) redisTemplate.opsForValue().get(key);
	}
	
	/**
	 * 移除單個String類型key
	 *
	 * @param key 
	 * @author shy
	 * @date 2018年11月13日 上午10:42:01
	 */
	public void removeSingleStringKey(String key) {
		if (StringUtils.isBlank(key)) {
			throw new EmptyParameterException();
		}
		stringRedisTemplate.opsForValue().getOperations().delete(key);
	}
	
	/**
	 * 移除Collection<String>類型keys
	 *
	 * @param keys 
	 * @author shy
	 * @date 2018年11月13日 下午3:15:16
	 */
	public void removeMultiStringKey(Collection<String> keys) {
		if (CollectionUtils.isNotEmpty(keys)) {
			stringRedisTemplate.opsForValue().getOperations().delete(keys);
		}
	}
	
	/**
	 * redis key 模糊查詢
	 * @author shy
	 * @date 2021年1月4日 上午11:21:45
	 * @param key
	 * @return
	 */
	public Set<String> queryStringKeys(String key) {
		 return redisTemplate.keys(key + "*");
	}
}

redis過期策略功能介紹

我們在使用redis時,一般會設(shè)置一個過期時間,當然也有不設(shè)置過期時間的,也就是永久不過期。

當我們設(shè)置了過期時間,redis是如何判斷是否過期,以及根據(jù)什么策略來進行刪除的。

設(shè)置過期時間

我們set key的時候,可以給一個expire time,就是過期時間,指定這個key比如說只能存活一個小時,假設(shè)你設(shè)置一批key存活一小時,那么接下來一小時后,redis是如何對這批key進行刪除的?

答案是:定期刪除+惰性刪除。

所謂定期刪除是指redis默認每隔100ms就隨機抽取一些設(shè)置了過期時間的key,檢查其是否過期,如果過期就刪除。注意這里可不是每隔100ms就遍歷所有的設(shè)置過期時間的key,那樣就是一場性能上的災難。實際上redis是每隔100ms隨機抽取一些key來檢查和刪除的。

但是問題是定期刪除可能會導致很多過期key到了時間并沒有被刪除,所以要惰性刪除,就是說在你獲取某個key的時候,redis會檢查一下,這個key如果設(shè)置了過期時間那么是否過期了?如果過期了就會刪除。

通過上述兩種手段結(jié)合起來,保證過期的key一定會被干掉。所以并不是到了過期時間就會將所有的過期key的刪除掉,這也是到了過期時間內(nèi)存占用并不會降低的原因。

但是實際上這還是有問題的,如果定期刪除漏掉了很多過期key,然后沒有及時去查也就沒走惰性刪除,就會導致大量過期key堆積在內(nèi)存里耗費redis內(nèi)存,這種情況如何處理?

答案是:走內(nèi)存淘汰機制。

內(nèi)存淘汰

如果redis的內(nèi)存占用過多的時候,此時會進行一些淘汰,有如下一些策略:

  • noeviction:當內(nèi)存不足以容納新寫入數(shù)據(jù)時,新寫入數(shù)據(jù)會報錯,這個實際場景一般不會使用。
  • allkeys-lru:當內(nèi)存不足以容納新寫入數(shù)據(jù)時,在鍵空間中,移除最少使用的key(這個是最常用的)
  • allkeys-random:當內(nèi)存不足以容納新寫入數(shù)據(jù)時,在鍵空間中,隨機移除某個key,這個一般用的比較少。
  • volatile-lru:當內(nèi)存不足以容納新寫入數(shù)據(jù)時,在設(shè)置了過期時間的鍵空間中,移除最近最少使用的key。
  • volatile-random:當內(nèi)存不足以容納新寫入數(shù)據(jù)時,在設(shè)置了過期時間的鍵空間中,隨機移除某個key。
  • volatile-ttl:當內(nèi)存不足以容納新寫入數(shù)據(jù)時,在設(shè)置了過期時間的鍵空間中,有更早過期時間的key優(yōu)先移除。

內(nèi)存淘汰會觸發(fā)淘汰條件刪除某些key,這也是造成key沒有設(shè)置過期時間而丟失的原因。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論