Java实现简单本地缓存
2018-09-04 本文已影响22人
小炉炉
项目用到的最简单的版本的本地缓存,因为目前我所涉及到的项目还没有用到分布式缓存,只是简单的单机版的本地缓存,利用的是HashMap的方式来实现存储的,然后再加上定时任务,来实现固定频率的本地缓存更新。目的就是为了让前端数据从缓存中进行获取。核心的实现方式就是key - value 的实现方式。
本地缓存的java代码:
1.定义CacheEntity.java的model类,来存放value和时间戳以及过期时间(属性的get和set方法需要自行实现)
public class CacheEntity implements Serializable {
private static final long serialVersionUID = 7172649826282703560L;
/**
* 值
*/
private Object value;
/**
* 保存的时间戳
*/
private long gmtModify;
/**
* 过期时间
*/
private int expire;
public CacheEntity(Object value, long gmtModify, int expire) {
super();
this.value = value;
this.gmtModify = gmtModify;
this.expire = expire;
}
}
2.定义LocalCache.java工具类,通过这个工具类来将key-value形式的代码存放在本地缓存中。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;
public class LocalCache {
// 默认的缓存容量
private static int DEFAULT_CAPACITY = 512;
// 最大容量
private static int MAX_CAPACITY = 100000;
// 刷新缓存的频率
private static int MONITOR_DURATION = 2;
// 启动监控线程
static {
new Thread(new TimeoutTimerThread()).start();
}
// 使用默认容量创建一个Map
private static LinkedHashMap<String, CacheEntity> cache = new LinkedHashMap<String, CacheEntity>(
DEFAULT_CAPACITY);
private LocalCache() {
}
/**
* 将key-value 保存到本地缓存并制定该缓存的过期时间
*
* @param key
* @param value
* @param expireTime
* 过期时间,如果是-1 则表示永不过期
* @return
*/
public static boolean putValue(String key, Object value, int expireTime) {
return putCloneValue(key, value, expireTime);
}
/**
* 将值通过序列化clone 处理后保存到缓存中,可以解决值引用的问题
*
* @param key
* @param value
* @param expireTime
* @return
*/
private static boolean putCloneValue(String key, Object value, int expireTime) {
try {
if (cache.size() >= MAX_CAPACITY) {
return false;
}
// 序列化赋值
CacheEntity entityClone = clone(new CacheEntity(value,
System.nanoTime(), expireTime));
cache.put(key, entityClone);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
*
* 序列化 克隆处理
*
* @param object
* @return
*/
private static <T extends Serializable> T clone(T object) {
T cloneObject = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
cloneObject = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return cloneObject;
}
/**
* 从本地缓存中获取key对应的值,如果该值不存则则返回null
*
* @param key
* @return
*/
public static Object getValue(String key) {
return cache.get(key).getValue();
}
/**
* 清空所有
*/
public void clear() {
cache.clear();
}
/**
* 过期处理线程
*
* @author Lenovo
* @version $Id: LocalCache.java, v 0.1 2014年9月6日 下午1:34:23 Lenovo Exp $
*/
static class TimeoutTimerThread implements Runnable {
public void run() {
while (true) {
try {
System.out.println("Cache monitor");
TimeUnit.SECONDS.sleep(MONITOR_DURATION);
checkTime();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 过期缓存的具体处理方法
*
* @throws Exception
*/
private void checkTime() throws Exception {
// "开始处理过期 ";
for (String key : cache.keySet()) {
CacheEntity tce = cache.get(key);
long timoutTime = TimeUnit.NANOSECONDS.toSeconds(System
.nanoTime() - tce.getGmtModify());
// " 过期时间 : "+timoutTime);
if (tce.getExpire() > timoutTime) {
continue;
}
System.out.println(" 清除过期缓存 : " + key);
// 清除过期缓存和删除对应的缓存队列
cache.remove(key);
}
}
}
}
里面的方法根据不同的需要使用不同的方法,代码是从博主的文章中摘抄的,不过我做了一些修改,因为我们公司用的是jdk1.7 所以我把concrrountHashMap更换成了 LinkedHashMap()
定时任务
这里我使用的SSH框架,这里特别对于初学者来说。需要注意两点,一是必须在Spring的配置文件中启用这个
<task:annotation-driven />
然后需要另外创建一个新的定时类,加上@Scheduled注解之后,其所注解的方法才会 生效,直接在代码的方法中加入@Scheduled注解是不能生效的
定时类如下:
@Component
public class TimerDuty {
private static Logger log = Logger.getLogger(TimerDuty.class);
@Autowired
FacilityService facilityService;
/**
* 定时任务,每2分钟执行一次操作,从透传云获取数据
*/
@Scheduled(cron="2 * * * * ?")
public void run(){
facilityService.setTypeToLocalCache();
log.info("TimerDuty run is starting");
}
/**
* 定时任务,每2分钟执行一次操作,从透传云获取数据
*/
@Scheduled(cron="2 * * * * ?")
public void runs(){
facilityService.setHisToLocakCache();
log.info("TimerDuty runs is starting");
}
}
该类需要被加入到Spring容器中才行,这样,我才能通过@Autoward方法将类。注入进来,从而调用方法。可能手段低级如果想要吐槽或者指出错误请留言。