📄 defaultcache.java
字号:
package zhouyouguo.slack;
import java.util.*;
/** *//**
* 系统默认的缓存
* @author jgao
*
*/
class DefaultCache implements ICacheInterface {
static int FreshTimerIntervalSeconds = 1; //缓存中对象生命周期的频率(一秒)
Map datas;
// Map<String, SimpleCacheInfo> datas; //缓存容器
private Timer timer; //时间任务
/** *//**
* 默认构造函数
*
*/
public DefaultCache() {
//实例化有防止线程同步操作的缓存容器
datas = Collections.synchronizedMap(new HashMap());
// datas = Collections.synchronizedMap(new HashMap<String, SimpleCacheInfo>());
//刷新缓存
TimerTask task = new CacheFreshTask(this);
timer = new Timer();
// timer = new Timer("SimpleCache_Timer", true);
timer.scheduleAtFixedRate(task, 1000, FreshTimerIntervalSeconds * 1000);//每格一秒刷新一次(缓存中对象的生命周期减一)
}
/** *//**
* 判断缓存中的对象是否存在
* @param key
* @return
*/
public boolean contains(String key){
return datas.containsKey(key);
}
/** *//**
* 获取缓存中的对象
* @param key 对象名称
* @return
*/
public Object get(String key) {
if (datas.containsKey(key)) {
SimpleCacheInfo sci = (SimpleCacheInfo)datas.get(key);
//sci.setSecondsRemain(sci.getSecondsTotal());
return sci.getObj();
}
return null;
}
/** *//**
* 向缓存中插入对象
* @param key 对象名称
* @param obj 对象
* @param cacheSeconds 对象在缓存中存在的时间
*/
public void Insert(String key, Object obj, int cacheSeconds) {
Add(key, obj, cacheSeconds);
}
/** *//**
*
* 向缓存中添加对象,并返回该对象
* @param key 对象名称
* @param obj 对象
* @param cacheSeconds 对象在缓存中存在的时间
* @return
*/
public Object Add(String key, Object obj, int cacheSeconds) {
if (cacheSeconds != 0) {
SimpleCacheInfo sci = new SimpleCacheInfo(obj, cacheSeconds);
datas.put(key, sci);
}
return obj;
}
/** *//**
* 移除缓存中的对象
* @param key 对象名称
* @return
*/
public Object Remove(String key) {
//SimpleCacheInfo sci = datas.remove(key);
SimpleCacheInfo sci = (SimpleCacheInfo) datas.remove(key);
if (sci != null) {
return sci.getObj();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -