redis中的Expire命令用于设置 key 的过期时间,key 过期后将不再可用。不过有些变量我们不需要设置到redis中,只要存到本地就可以了,也需要过期时间,也可以同样方法定义一个全局变量的map之后键是变量,值是时间每次都进行比较。
public class ExpireModel<T> {
/**
* 值
*
*/
private T value;
/**
* 值设置时间(时间戳,毫秒)
*
* @author ybwei
*/
private Long setTime;
/**
* 过期时长(毫秒)
*/
private Long expireTime;
/**
* @param value
* @param expireTime
*/
public ExpireModel(T value, Long expireTime) {
super();
this.value = value;
this.expireTime = expireTime;
this.setTime = System.currentTimeMillis();
}
/**
* @Description:
* @return
*/
public T getValue() {
T result = null;
if (System.currentTimeMillis() - this.setTime <= this.expireTime) {
result = this.value;
}
return result;
}
}
/**
* @throws InterruptedException
* @Description:
*/
@Test
public void testExpire() throws InterruptedException {
// 失效时间1秒
ExpireModel<String> em = new ExpireModel<>("aa", 1000L);
log.info("value:{}", em.getValue());
// 休眠时间2秒
TimeUnit.SECONDS.sleep(2);
log.info("value:{}", em.getValue());
}
结果展示:
[INFO ] 2022-08-27 11:32:46.680 [main] com.spring.pro.test.model.ExpireTest - value:aa
[INFO ] 2022-08-27 11:32:48.692 [main] com.spring.pro.test.model.ExpireTest - value:null
import javax.annotation.PostConstruct;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spring.pro.model.ExpireModel;
@RestController
public class IndexController {
private ExpireModel<String> em = null;
/**
* 失效时间(2秒)
*
*/
private Long expireTime = 10 * 1000L;
private int index = 0;
/**
* @Description:
* @Author: ybwei
* @Date: 2019年12月25日 上午11:14:58
*/
@PostConstruct
public void init() {
em = new ExpireModel<String>("aa", expireTime);
}
/**
* @Description:
* @return
*/
@GetMapping("/getValue")
public String getValue() {
String value = em.getValue();
if (value == null) {
value="bb" + index;
em = new ExpireModel<String>(value, expireTime);
index++;
}
return value;
}
}
4 使用HashMap方法实现
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class CacheService {
private Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
/**
* 设置变量值
* @param key
* @param ex 保存的时间毫秒
* @param value 变量值
*/
public void setex(String key, Long ex, String value){
Map<String, Object> m = new HashMap<String,Object>();
m.put("ex", ex); //剩余的时间毫秒
m.put("extime", new Date().getTime() + ex); //具体过期的时间戳毫秒
m.put("value", value);
map.put(key, m);
}
public String get(String key){
expor(key);
if(map.containsKey(key)){
Map<String, Object> obj = map.get(key);
return String.valueOf(obj.get("value"));
}
return null;
}
/**
* 计算剩余时间
* @param key
*/
private void expor(String key){
if(map.containsKey(key)){
Map<String, Object> obj = map.get(key);
Long ex = (Long) obj.get("ex");
Long extime = (Long) obj.get("extime");
if(extime < new Date().getTime()){
map.remove(key);
}else{
obj.put("ex", extime - new Date().getTime());
map.put(key, obj);
}
}
}
/**
* 获取变量剩余时间
* @param key
* @return
*/
public Long ttl(String key){
expor(key);
if(map.containsKey(key)){
return (Long) map.get(key).get("ex");
}
return null;
}
}