Java操作redis設(shè)置第二天凌晨過期的解決方案
Java操作redis設(shè)置第二天凌晨過期
場景
在做查詢數(shù)據(jù)的時(shí)候,遇到了需要設(shè)置數(shù)據(jù)在redis中第二天過期的問題,但是redis又沒有對應(yīng)的API,就只好自己來解決了
思路
計(jì)算出第二天凌晨與當(dāng)前時(shí)間的時(shí)間差,將該時(shí)間差設(shè)置為redis的過期時(shí)間,就可以達(dá)到我們想要的效果
代碼
/**
? ? ?* 計(jì)算第二天凌晨與當(dāng)前時(shí)間的時(shí)間差秒數(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;
? ? }拿到了時(shí)間差,剩下的基本上就沒什么問題了。
附上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,過期時(shí)間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的過期時(shí)間
*
* @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的過期時(shí)間
*
* @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,過期時(shí)間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);
}
/**
* 移除單個(gè)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í),一般會(huì)設(shè)置一個(gè)過期時(shí)間,當(dāng)然也有不設(shè)置過期時(shí)間的,也就是永久不過期。
當(dāng)我們設(shè)置了過期時(shí)間,redis是如何判斷是否過期,以及根據(jù)什么策略來進(jìn)行刪除的。
設(shè)置過期時(shí)間
我們set key的時(shí)候,可以給一個(gè)expire time,就是過期時(shí)間,指定這個(gè)key比如說只能存活一個(gè)小時(shí),假設(shè)你設(shè)置一批key存活一小時(shí),那么接下來一小時(shí)后,redis是如何對這批key進(jìn)行刪除的?
答案是:定期刪除+惰性刪除。
所謂定期刪除是指redis默認(rèn)每隔100ms就隨機(jī)抽取一些設(shè)置了過期時(shí)間的key,檢查其是否過期,如果過期就刪除。注意這里可不是每隔100ms就遍歷所有的設(shè)置過期時(shí)間的key,那樣就是一場性能上的災(zāi)難。實(shí)際上redis是每隔100ms隨機(jī)抽取一些key來檢查和刪除的。
但是問題是定期刪除可能會(huì)導(dǎo)致很多過期key到了時(shí)間并沒有被刪除,所以要惰性刪除,就是說在你獲取某個(gè)key的時(shí)候,redis會(huì)檢查一下,這個(gè)key如果設(shè)置了過期時(shí)間那么是否過期了?如果過期了就會(huì)刪除。
通過上述兩種手段結(jié)合起來,保證過期的key一定會(huì)被干掉。所以并不是到了過期時(shí)間就會(huì)將所有的過期key的刪除掉,這也是到了過期時(shí)間內(nèi)存占用并不會(huì)降低的原因。
但是實(shí)際上這還是有問題的,如果定期刪除漏掉了很多過期key,然后沒有及時(shí)去查也就沒走惰性刪除,就會(huì)導(dǎo)致大量過期key堆積在內(nèi)存里耗費(fèi)redis內(nèi)存,這種情況如何處理?
答案是:走內(nèi)存淘汰機(jī)制。
內(nèi)存淘汰
如果redis的內(nèi)存占用過多的時(shí)候,此時(shí)會(huì)進(jìn)行一些淘汰,有如下一些策略:
noeviction:當(dāng)內(nèi)存不足以容納新寫入數(shù)據(jù)時(shí),新寫入數(shù)據(jù)會(huì)報(bào)錯(cuò),這個(gè)實(shí)際場景一般不會(huì)使用。allkeys-lru:當(dāng)內(nèi)存不足以容納新寫入數(shù)據(jù)時(shí),在鍵空間中,移除最少使用的key(這個(gè)是最常用的)allkeys-random:當(dāng)內(nèi)存不足以容納新寫入數(shù)據(jù)時(shí),在鍵空間中,隨機(jī)移除某個(gè)key,這個(gè)一般用的比較少。volatile-lru:當(dāng)內(nèi)存不足以容納新寫入數(shù)據(jù)時(shí),在設(shè)置了過期時(shí)間的鍵空間中,移除最近最少使用的key。volatile-random:當(dāng)內(nèi)存不足以容納新寫入數(shù)據(jù)時(shí),在設(shè)置了過期時(shí)間的鍵空間中,隨機(jī)移除某個(gè)key。volatile-ttl:當(dāng)內(nèi)存不足以容納新寫入數(shù)據(jù)時(shí),在設(shè)置了過期時(shí)間的鍵空間中,有更早過期時(shí)間的key優(yōu)先移除。
內(nèi)存淘汰會(huì)觸發(fā)淘汰條件刪除某些key,這也是造成key沒有設(shè)置過期時(shí)間而丟失的原因。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java分析html算法(java網(wǎng)頁蜘蛛算法示例)
近來有些朋友在做蜘蛛算法,或者在網(wǎng)頁上面做深度的數(shù)據(jù)挖掘,下面使用示例2014-03-03
SpringCloud集成zookeeper實(shí)現(xiàn)服務(wù)注冊并訪問功能
zookeeper和eureka一樣,是用于充當(dāng)服務(wù)注冊功能服務(wù)器的一個(gè)springcloud插件,這篇文章主要介紹了SpringCloud集成zookeeper實(shí)現(xiàn)服務(wù)注冊并訪問,需要的朋友可以參考下2022-06-06
MyBatis解決Update動(dòng)態(tài)SQL逗號(hào)的問題
這篇文章主要介紹了MyBatis解決Update動(dòng)態(tài)SQL逗號(hào)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring Web零xml配置原理以及父子容器關(guān)系詳解
這篇文章主要介紹了Spring Web零xml配置原理以及父子容器關(guān)系詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
使用java自帶des加密算法實(shí)現(xiàn)文件加密和字符串加密
這篇文章主要介紹了使用java自帶des加密算法實(shí)現(xiàn)文件加密和字符串加密的示例,需要的朋友可以參考下2014-03-03
MyBatis攔截器:給參數(shù)對象屬性賦值的實(shí)例
下面小編就為大家?guī)硪黄狹yBatis攔截器:給參數(shù)對象屬性賦值的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Java?Stream如何將List分組成Map或LinkedHashMap
這篇文章主要給大家介紹了關(guān)于Java?Stream如何將List分組成Map或LinkedHashMap的相關(guān)資料,stream流是Java8的新特性,極大簡化了集合的處理操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12

