CacheMap缓存的多种方法

(一)

Map<String,List<String>> cache = new ConcurrentHashMap<String, List<String>>();
cache.remove("str");
cache.put("str",getCmsCfgAll());
List<String> name = cache.get("str");
(二)
package com.jdjinsui.controlservice.tool;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 缓存管理类
* Created by zxy on 2017/5/16.
*/
public class CacheMgr {
private static Map cacheMap = new HashMap();
private static Map cacheConfMap = new HashMap();

private static CacheMgr cm = null;

//构造方法
private CacheMgr(){
}

public static CacheMgr getInstance(){
if(cm==null){
cm = new CacheMgr();
Thread t = new ClearCache();
t.start();
}
return cm;
}


/**
* 增加缓存
* @param key
* @param value
* @param ccm 缓存对象
* @return
*/
public static boolean addCache(Object key,Object value,CacheConfModel ccm){
System.out.println("开始增加缓存-------------");
boolean flag = false;
try {
cacheMap.put(key, value);
cacheConfMap.put(key, ccm);
System.out.println("增加缓存结束-------------");
System.out.println("now addcache=="+cacheMap.size());
flag=true;
} catch (Exception e) {
e.printStackTrace();
}

return flag;
}


/**
* 获取缓存实体
*/
public static Object getValue(String key){
Object ob=cacheMap.get(key);
if(ob!=null){
return ob;
}else{
return null;
}
}


/**
* 获取缓存数据的数量
* @return
*/
public static int getSize(){
return cacheMap.size();
}




/**
* 删除缓存
* @param key
* @return
*/
public static boolean removeCache(Object key){
boolean flag=false;
try {
cacheMap.remove(key);
cacheConfMap.remove(key);
flag=true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}



/**
* 清除缓存的类
* 继承Thread线程类
*/
private static class ClearCache extends Thread{
public void run(){
while(true){
Set tempSet = new HashSet();
Set set = cacheConfMap.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
Object key = it.next();
CacheConfModel ccm = (CacheConfModel)cacheConfMap.get(key);
//比较是否需要清除
if(!ccm.isForever()){
if((new Date().getTime()-ccm.getBeginTime())>= ccm.getDurableTime()*60*1000){
//可以清除,先记录下来
tempSet.add(key);
}
}
}
//真正清除
Iterator tempIt = tempSet.iterator();
while(tempIt.hasNext()){
Object key = tempIt.next();
cacheMap.remove(key);
cacheConfMap.remove(key);

}
System.out.println("now thread================>"+cacheMap.size());
//休息
try {
Thread.sleep(60*1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.jdjinsui.controlservice.tool;

/**
* 缓存属性类
* Created by zxy on 2017/5/17.
*/
public class CacheConfModel implements java.io.Serializable {
private long beginTime;//缓存开始时间
private boolean isForever = false;//是否持久
private int durableTime;//持续时间

public long getBeginTime() {
return beginTime;
}
public void setBeginTime(long beginTime) {
this.beginTime = beginTime;
}
public boolean isForever() {
return isForever;
}
public void setForever(boolean isForever) {
this.isForever = isForever;
}
public int getDurableTime() {
return durableTime;
}
public void setDurableTime(int durableTime) {
this.durableTime = durableTime;
}
}
//测试方法
Gson gson=new Gson();
CacheMgr cm=CacheMgr.getInstance();
CacheConfModel cModel=new CacheConfModel();
Date d=new Date();
cModel.setBeginTime(d.getTime());
cModel.setDurableTime(60);
cModel.setForever(true);
cm.addCache("cms", getCmsCfgAll(), cModel);//在缓存加值

CacheMgr cn=CacheMgr.getInstance();
String js=gson.toJson(cn.getValue("cms"));
List<CmsCfg> user=gson.fromJson(js,new TypeToken<List<CmsCfg>>() {}.getType());

(三)
package com.jdjinsui.controlservice.tool;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Created by zxy on 2017/5/17.
*/
public class CacheMap<K, V> extends AbstractMap<K, V> {
private static final long DEFAULT_TIMEOUT = 30000;
private static CacheMap<Object, Object> defaultInstance;

public static synchronized final CacheMap<Object, Object> getDefault() {
if (defaultInstance == null) {
defaultInstance = new CacheMap<Object, Object>(DEFAULT_TIMEOUT);
}
return defaultInstance;
}

private class CacheEntry implements Entry<K, V> {
long time;
V value;
K key;

CacheEntry(K key, V value) {
super();
this.value = value;
this.key = key;
this.time = System.currentTimeMillis();
}

@Override
public K getKey() {
return key;
}

@Override
public V getValue() {
return value;
}

@Override
public V setValue(V value) {
return this.value = value;
}
}

private class ClearThread extends Thread {
ClearThread() {
setName("clear cache thread");
}

public void run() {
while (true) {
try {
long now = System.currentTimeMillis();
Object[] keys = map.keySet().toArray();
for (Object key : keys) {
CacheEntry entry = map.get(key);
if (now - entry.time >= cacheTimeout) {
synchronized (map) {
map.remove(key);
}
}
}
Thread.sleep(cacheTimeout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

private long cacheTimeout;
private Map<K, CacheEntry> map = new HashMap<K, CacheEntry>();

public CacheMap(long timeout) {
this.cacheTimeout = timeout;
new ClearThread().start();
}

@Override
public Set<Entry<K, V>> entrySet() {
Set<Entry<K, V>> entrySet = new HashSet<Map.Entry<K, V>>();
Set<Entry<K, CacheEntry>> wrapEntrySet = map.entrySet();
for (Entry<K, CacheEntry> entry : wrapEntrySet) {
entrySet.add(entry.getValue());
}
return entrySet;
}

@Override
public V get(Object key) {
CacheEntry entry = map.get(key);
return entry == null ? null : entry.value;
}

@Override
public V put(K key, V value) {
CacheEntry entry = new CacheEntry(key, value);
synchronized (map) {
map.put(key, entry);
}
return value;
}
}

 

posted on 2017-05-17 14:29  幻影鬼火  阅读(332)  评论(0编辑  收藏  举报

导航