说明:
java本地缓存的使用,方便快捷
代码:
1、新建一个Cache实体
package com.jdc.cache;
public class Cache {
private String key;//缓存ID
private Object value;//缓存数据
private long timeOut;//更新时间
private boolean expired; //是否终止
public Cache() {
super();
}
public Cache(String key, Object value, long timeOut, boolean expired) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
this.expired = expired;
}
public String getKey() {
return key;
}
public long getTimeOut() {
return timeOut;
}
public Object getValue() {
return value;
}
public void setKey(String string) {
key = string;
}
public void setTimeOut(long l) {
timeOut = l;
}
public void setValue(Object object) {
value = object;
}
public boolean isExpired() {
return expired;
}
public void setExpired(boolean b) {
expired = b;
}
}
2、管理缓存
package com.jdc.cache;
import java.util.HashMap;
//Description: 管理缓存
//可扩展的功能:当chche到内存溢出时必须清除掉最早期的一些缓存对象,这就要求对每个缓存对象保存创建时间
public class CacheManager {
private volatile static HashMap cacheMap ;
//单实例构造方法
private CacheManager() {
super();
}
public static HashMap getCache() {
if (cacheMap == null) {
synchronized (HashMap.class) {
if (cacheMap == null) {
cacheMap = new HashMap();
}
}
}
return cacheMap;
}
//得到缓存。同步静态方法
private synchronized static Cache getCache(String key) {
return (Cache) getCache().get(key);
}
//判断是否存在一个缓存
public synchronized static boolean hasCache(String key) {
return getCache().containsKey(key);
}
//获取缓存信息
public static Cache getCacheInfo(String key) {
if (hasCache(key)) {
Cache cache = getCache(key);
if (cacheExpired(cache)) { //调用判断是否终止方法
cache.setExpired(true);
return null;
}
return cache;
}else
return null;
}
//重写载入缓存信息方法
public static void putCacheInfo(String key,Object obj,long dt){
Cache cache = new Cache();
cache.setKey(key);
if(dt<0){
cache.setTimeOut(dt);
}else {
cache.setTimeOut(dt+System.currentTimeMillis());
}
cache.setValue(obj);
cache.setExpired(false);
getCache().put(key,cache);
}
//判断缓存是否终止
public static boolean cacheExpired(Cache cache) {
if (null == cache) { //传入的缓存不存在
return false;
}
long nowDt = System.currentTimeMillis(); //系统当前的毫秒数
long cacheDt = cache.getTimeOut(); //缓存内的过期毫秒数
if (cacheDt <= 0||cacheDt>nowDt) { //过期时间小于等于零时,或者过期时间大于当前时间时,则为FALSE
return false;
} else { //大于过期时间 即过期
return true;
}
}
}
3、测试缓存
package com.jdc.cache;
public class Test {
public static void main(String[] args){
CacheManager.putCacheInfo("pda", "test", 1000*60*60);
System.out.println("缓存已存入");
Cache cache = CacheManager.getCacheInfo(String.valueOf("pda"));
String value = (String)cache.getValue();
System.out.println(value);
}
}