简单缓存实现
来源:互联网 发布:人种 知乎 编辑:程序博客网 时间:2024/06/05 16:09
实现:添加内容到缓存,删除缓存内容,每隔一段时间就自动清空缓存
感谢百度pkzahc 用户的支持,本文纯模仿
public class CacheMgr {
private static Map cacheMap = new HashMap();private static Map cacheConfMap = new HashMap();
private static Integer initCount = 0;
private CacheMgr(){
}
private static CacheMgr cm = null;
public static CacheMgr getInstance(){
if(cm==null){
System.out.println("init -- "+initCount);
initCount++;
cm = new CacheMgr();
Thread t = new ClearCache();
t.start();
}
return cm;
}
/**
* 增加缓存
* @param key
* @param value
* @param ccm 缓存对象
* @return
*/
public boolean addCache(Object key,Object value,CacheConfModel ccm){
boolean flag = false;
cacheMap.put(key, value);
cacheConfMap.put(key, ccm);
System.out.println("now addcache=="+cacheMap.size());
return true;
}
/**
* 删除缓存
* @param key
* @return
*/
public boolean removeCache(Object key){
cacheMap.remove(key);
cacheConfMap.remove(key);
System.out.println("now removeCache=="+cacheMap.size());
return true;
}
/**
* 清除缓存的类
* @author wanglj
* 继承Thread线程类
*/
private static class ClearCache extends Thread{
public void run(){
Thread().currentThread().setName("hello");
int i = 0;
while(true){
System.out.println(i);
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(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
private Thread Thread() {
// TODO Auto-generated method stub
return null;
}
}
public static void main(String[] args){
CacheMgr cm = CacheMgr.getInstance();
CacheConfModel ccm = new CacheConfModel();
ccm.setBeginTime(new Date().getTime());
ccm.setDurableTime(2);
cm.addCache("A", "A", ccm);
Map map=Thread.getAllStackTraces();//获取当前运行的所有线程
System.out.println(map.size());
Set sets = map.entrySet();
Iterator it = sets.iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
Thread t = (Thread) entry.getKey();
System.out.println(t.getId()+":"+t.getName());
}
//System.exit( 0 );
}
}
public class CacheConfModel {
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;
}
}
- 简单缓存实现
- 简单缓存的实现
- 缓存池-简单实现
- 简单实现本地缓存
- map实现简单缓存
- 简单LRU算法实现缓存
- HASHMAP缓存简单java实现
- java缓存的简单实现
- Lru缓存的简单实现
- 简单LRU算法实现缓存
- 简单的LRU缓存实现
- 简单的java缓存实现
- 简单的java缓存实现
- 简单LRU算法实现缓存
- 简单LRU算法实现缓存
- 用Map实现简单缓存
- 缓存的简单实现例子
- Android 简单实现缓存机制
- Java中堆与栈的区别分析
- session_start(): Failed to initialize storage module 解决办法
- 如何限定QTextEdit中字数的个数
- octopress博客搭建和个性化配置
- 一个字符数组,里面的字符可能是a-z、A-Z、0-9.现在要求对数组进行排序,要求所有小写字符放在最前面,所有大写字符放在中间,所有数字放在最后,而且各部分内部分别有序(创新工场)
- 简单缓存实现
- OCX控件的注册卸载,以及判断是否注册
- 设置ScrollView的圆角
- 无题
- Vmware虚拟机下三种网络模式配置
- Python 调用C
- python3实现的多线程httpserver
- c语言中关于圆括号中以逗号赋值
- C# 单例模式 启动画面