框架基础____缓存框架基本的原理_一个简单的缓存工具类CacheUtil

来源:互联网 发布:上海市政务数据资源网 编辑:程序博客网 时间:2024/05/21 10:00
package com.lp.frame.base.utils.cache;import java.io.Serializable;import java.util.Iterator;import java.util.Map.Entry;import java.util.Timer;import java.util.TimerTask;import java.util.concurrent.ConcurrentHashMap;/** * @author Administrator * 缓存工具类 */public class CacheUtil implements Serializable {/** * SERID */private static final long serialVersionUID = -5393496161251379029L;/** * 缓存集合 */private static ConcurrentHashMap<String, CacheValue> mapCache = new ConcurrentHashMap<String, CacheValue>();private static Timer timer;private static byte[] lock = new byte[0];private static boolean isRun = false;//没隔多少秒清理一次  6秒private static Long DEFAULT_TASK_PERIOD = 1000L * 6 ;//该key的值超时时间为 30秒private static Long DEFAULT_TIMEOUT = 1000L * 30 ;private static CacheUtil instance;private CacheUtil() {}public static CacheUtil getInstance() {synchronized (lock) {if (instance == null) {instance = new CacheUtil();timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {clearCache();}}, 0, DEFAULT_TASK_PERIOD);}}return instance;}public Object put(String key, Object value,long timeout) {CacheValue cvalue = new CacheValue();   if(key != null){   cvalue.setKey(key);   }   cvalue.setValue(value);   if(timeout != 0){   cvalue.setTimeout(timeout);   }else{   cvalue.setTimeout(DEFAULT_TIMEOUT);   }   cvalue.setCreateTime(System.currentTimeMillis());return mapCache.put(key, cvalue);}public Object get(String key) {if (key != null) {Object obj = null;if (mapCache.containsKey(key)) {obj = mapCache.get(key);}if (obj != null) {CacheValue cvalue = (CacheValue) obj;return cvalue.getValue();}}return null;}public boolean containsKey(String key) {return mapCache.containsKey(key);}private static void clearCache() {if (!isRun) {synchronized (lock) {try {isRun = true;if (!mapCache.isEmpty()) {System.out.println("移除前:"+mapCache);Iterator<Entry<String, CacheValue>> iterator = mapCache.entrySet().iterator();while (iterator.hasNext()) {Entry<String, CacheValue> entry = iterator.next();CacheValue cacheValue = entry.getValue();if (cacheValue.isTimeout()) {mapCache.remove(entry.getKey());System.out.println("移除超时__:"+entry.getKey());}}System.out.println("剩余__:"+mapCache);}} catch (Exception e) {e.printStackTrace();}finally{isRun = false;}}}}class CacheValue implements Serializable {private static final long serialVersionUID = 800800964682488492L;private String key;private Object value;private Long createTime;private Long timeout;public String getKey() {return key;}public void setKey(String key) {this.key = key;}public Long getCreateTime() {return createTime;}public void setCreateTime(Long createTime) {this.createTime = createTime;}public Long getTimeout() {return timeout;}public void setTimeout(Long timeout) {this.timeout = timeout;}public Object getValue() {isTimeout();return value;}public void setValue(Object value) {this.value = value;}public boolean isTimeout() {boolean flag = true;if (value != null) {flag = System.currentTimeMillis() - createTime >= timeout;if (flag) {value = null;}}return flag;}}}

//测试类

package com.lp.frame.base.utils.cache.test;import com.lp.frame.base.utils.cache.CacheUtil;public class Test {public static void main(String[] args) {//1.初始化缓存对象CacheUtil  cacheUtil=CacheUtil.getInstance();  cacheUtil.put("user", "zhangsan", 1000L * 30);//30秒失效  cacheUtil.put("pwd", "zhangsan",1000L * 70);//70秒失效System.out.println(cacheUtil.get("user"));System.out.println(cacheUtil.get("pwd"));System.out.println(cacheUtil.containsKey("user"));System.out.println(cacheUtil.containsKey("pwd"));try {Thread.sleep(60000L); //1分钟休眠} catch (InterruptedException e) {e.printStackTrace();}//想得到的结果   user已经失效  但是pwd还可以获取到System.out.println("1分钟后"+cacheUtil.get("user"));System.out.println("1分钟后"+cacheUtil.get("pwd"));}}

//运行结构