redis定时过期,清除
我这里用的是redisTemplate控制的redis
可以发现他的put方法 除了key和value 还会放一个参数进去,就是他的失效时间
redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
只要往redis存值的时候,(指定当天失效时间 - System.currentTimeMillis() )/1000就会得到具体秒数
下面这个类用来获取指定时间的毫秒数
// An highlighted block
/**
* 获取特定时间的 毫秒数
*/
public static Long getMiol() {
Calendar ca = Calendar.getInstance();
//失效的时间
ca.set(Calendar.HOUR_OF_DAY, 23);
ca.set(Calendar.MINUTE, 30);
ca.set(Calendar.SECOND, 0);
long fl = ca.getTimeInMillis();
return fl;
}
接下来在put方法把获取失效秒数加入进去就好了
// An highlighted block
public void putTime(String key, String value) {
long seconds= (TimeLose.getMiol() - System.currentTimeMillis())/1000; //获取剩余时间
System.out.println(seconds);
redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
}