Java操作redis设置第二天凌晨过期的解决方案

这篇文章主要介绍了Java操作redis设置第二天凌晨过期的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Java操作redis设置第二天凌晨过期

场景

在做查询数据的时候,遇到了需要设置数据在redis中第二天过期的问题,但是redis又没有对应的API,就只好自己来解决了

思路

计算出第二天凌晨与当前时间的时间差,将该时间差设置为redis的过期时间,就可以达到我们想要的效果

代码

/**      * 计算第二天凌晨与当前时间的时间差秒数      * @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 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); } /** * 设置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); } /** *	设置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); } /** * 设置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 getObjectValue(String key, Class 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类型keys * * @param keys * @author shy * @date 2018年11月13日 下午3:15:16 */ public void removeMultiStringKey(Collection 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 queryStringKeys(String key) { return redisTemplate.keys(key + "*"); } } 

redis过期策略功能介绍

我们在使用redis时,一般会设置一个过期时间,当然也有不设置过期时间的,也就是永久不过期。

当我们设置了过期时间,redis是如何判断是否过期,以及根据什么策略来进行删除的。

设置过期时间

我们set key的时候,可以给一个expire time,就是过期时间,指定这个key比如说只能存活一个小时,假设你设置一批key存活一小时,那么接下来一小时后,redis是如何对这批key进行删除的?

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

所谓定期删除是指redis默认每隔100ms就随机抽取一些设置了过期时间的key,检查其是否过期,如果过期就删除。注意这里可不是每隔100ms就遍历所有的设置过期时间的key,那样就是一场性能上的灾难。实际上redis是每隔100ms随机抽取一些key来检查和删除的。

但是问题是定期删除可能会导致很多过期key到了时间并没有被删除,所以要惰性删除,就是说在你获取某个key的时候,redis会检查一下,这个key如果设置了过期时间那么是否过期了?如果过期了就会删除。

通过上述两种手段结合起来,保证过期的key一定会被干掉。所以并不是到了过期时间就会将所有的过期key的删除掉,这也是到了过期时间内存占用并不会降低的原因。

但是实际上这还是有问题的,如果定期删除漏掉了很多过期key,然后没有及时去查也就没走惰性删除,就会导致大量过期key堆积在内存里耗费redis内存,这种情况如何处理?

答案是:走内存淘汰机制。

内存淘汰

如果redis的内存占用过多的时候,此时会进行一些淘汰,有如下一些策略:

  • noeviction:当内存不足以容纳新写入数据时,新写入数据会报错,这个实际场景一般不会使用。
  • allkeys-lru:当内存不足以容纳新写入数据时,在键空间中,移除最少使用的key(这个是最常用的)
  • allkeys-random:当内存不足以容纳新写入数据时,在键空间中,随机移除某个key,这个一般用的比较少。
  • volatile-lru:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,移除最近最少使用的key。
  • volatile-random:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,随机移除某个key。
  • volatile-ttl:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,有更早过期时间的key优先移除。

内存淘汰会触发淘汰条件删除某些key,这也是造成key没有设置过期时间而丢失的原因。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持0133技术站。

以上就是Java操作redis设置第二天凌晨过期的解决方案的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java