描述
Java提供了多种本地缓存的实现方式,这里演示使用ConcurrentHashMap做本地缓存
ConcurrentHashMap:ConcurrentHashMap是HashMap的线程安全版本,它通过使用锁分段技术来提高并发性能。ConcurrentHashMap适用于多线程环境下的本地缓存需求,可以提供更好的并发性能。
定义缓存对象类
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @desc 缓存数据, 包含缓存值,创建时间,过期时间
**/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CacheValue {
//缓存值
Object value;
//创建时间,时间戳
Long createTime;
//过期时间,默认永不过期,单位为s
Long expireTime = -1L;
}
本地缓存操作类
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ConcurrentHashMap;
/**
* @desc 本地缓存操作类
**/
public class LocalCacheTemplate {
private static final Logger log = LoggerFactory.getLogger(LocalCacheTemplate.class);
private static final ConcurrentHashMap<String, CacheValue> cache = new ConcurrentHashMap<>();
public static final int mill = 1000;//1000ms,毫秒转换为秒
@SneakyThrows
public static void main(String[] args) {
set("name","yangjie",3);
System.out.println("the result is " + get("name"));
Thread.sleep(1000);
System.out.println("the result is " + get("name"));
Thread.sleep(3000);
System.out.println("the result is " + get("name"));
Thread.sleep(1000);
System.out.println("the result is " + get("name"));
Thread.sleep(1000);
System.out.println("the result is " + get("name"));
}
/**
* @desc 根据缓存key获取缓存值
* @param key 缓存key
* @return java.lang.Object 缓存值
**/
public static Object get(String key) {
if (!cache.containsKey(key)) {
return null;
}
CacheValue cacheValue = cache.get(key);
if (isOverTime(cacheValue)) {
log.debug("cache is over time");
cache.remove(key);//删除
return null;
} else {
return cacheValue.getValue();
}
}
/**
* @desc 根据缓存key获取缓存值
* @param key 缓存key
* @return CacheValue 缓存数据类
**/
public static CacheValue getCacheValue(String key) {
if (!cache.containsKey(key)) {
return null;
}
CacheValue cacheValue = cache.get(key);
if (isOverTime(cacheValue)) {
log.debug("cache is over time");
cache.remove(key);//删除
return null;
} else {
return cacheValue;
}
}
/**
* @desc 设置缓存值
* @param key 缓存key
* @param value 缓存值
*/
public static void set(String key,Object value) {
Long createTime = System.currentTimeMillis();
Long expireTime = -1l;
CacheValue cacheValue = new CacheValue();
cacheValue.setValue(value);
cacheValue.setCreateTime(createTime);
cacheValue.setExpireTime(expireTime);
cache.put(key,cacheValue);
}
/**
* @desc 设置缓存值,并且设置过期时间单位为秒
* @param key 缓存key
* @param value 缓存值
* @param expireTime 过期时间,单位为秒
*/
public static void set(String key,Object value,long expireTime) {
Long createTime = System.currentTimeMillis();
CacheValue cacheValue = new CacheValue();
cacheValue.setValue(value);
cacheValue.setCreateTime(createTime);
cacheValue.setExpireTime(expireTime);
cache.put(key,cacheValue);
}
/**
* @desc 判断是否过期
* @param cacheValue 缓存数据值
* @return boolean 是否过期
**/
private static boolean isOverTime(CacheValue cacheValue) {
Long createTime = cacheValue.getCreateTime();
Long expireTime = cacheValue.getExpireTime();
if(expireTime.equals(-1L)){
return false;
}
if ((System.currentTimeMillis() - createTime) / mill > expireTime) {
return true;
}
return false;
}
}