/**
* All rights Reserved, Designed By www.tydic.com
* @Title: CacheClass.java
* @Package com.andy.jcu.cachetest
* @Description: TODO(用一句话描述该文件做什么)
* @author: andyzhu
* @date: 2018年4月18日 下午6:48:27
* @version V1.0
* @Copyright: 2018 www.acc.com Inc. All rights reserved.
* 注意:禁止外泄以及用于其他的商业目
*/
package com.andy.jcu.cachetest;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import javax.naming.InitialContext;
/**
* @author sks
* cache缓存类,基于delayqueue,通过delayqueue存包含时间信息的key,map存key和value
* 为什么不用delayqueue存包含<key,val>的键值对呢。不好删除旧元素
*
*/
public class CacheClass {
/**
*
*/
public CacheClass() {
super();
init();
}
/**
* 建线程,不断的while循环操作delayqueue
*/
private void init() {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
DelayItem item = (DelayItem) queue.take();
//对于清出delayqueue队列的key,map中也要删除
map.remove(item.getKey());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
DelayQueue<Delayed>queue=new DelayQueue<>();
ConcurrentHashMap<String, String>map=new ConcurrentHashMap<>();
/*
* 在put时候要考虑情况:没有key的时候直接存。发现有key的时候,怎么办?有key的时候,要查找此key,value,
* 然后删除,然后重新添加
*/
public void putVal(String key,String val,long time){
DelayItem<String>item=new DelayItem<>(key, time*1000);
//首先在queue里找这个key
//如果找到此key,要删除此key,val
if (queue.contains(key)) {
queue.remove(key);
map.remove(key);
}
//然后重新添加
queue.put(item);
map.put(key, val);
}
public String getVal(String key){
return map.get(key);
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
CacheClass cache=new CacheClass();
cache.putVal("a", "andy", 100);
cache.putVal("b", "body", 2);
Thread.sleep(3000);
System.out.println("the val of a is "+cache.getVal("a"));
System.out.println("the val of b is "+cache.getVal("b"));
cache.putVal("a", "lucy", 2);
cache.putVal("b", "lili", 20);
Thread.sleep(3000);
System.out.println("the val of a is "+cache.getVal("a"));
System.out.println("the val of b is "+cache.getVal("b"));
}
}
class DelayItem<K> implements Delayed{
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public long getNow() {
return now;
}
public void setNow(long now) {
this.now = now;
}
public long getDelay() {
return delay;
}
public void setDelay(long delay) {
this.delay = delay;
}
public long getExpire() {
return expire;
}
public void setExpire(long expire) {
this.expire = expire;
}
/**
* @param key 键
* @param val 值
* @param expire 延时时间
*/
public DelayItem(String key, long expire) {
super();
this.key = key;
this.expire = expire+System.currentTimeMillis();
}
/**
*
*/
private void init() {
// TODO Auto-generated method stub
}
/**
* @param key
* @param val
* @param now
* @param delay
* @param expire
*/
public DelayItem(String key, long now, long delay, long expire) {
super();
this.key = key;
this.now = now;
this.delay = delay;
this.expire = expire;
}
private String key;
private long now; //创建时间
private long delay; //延迟时间
private long expire; //到期时间
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Delayed o) {
// TODO Auto-generated method stub
return (int) (this.getDelay(TimeUnit.MILLISECONDS)-o.getDelay(TimeUnit.MILLISECONDS));
}
/* (non-Javadoc)
* @see java.util.concurrent.Delayed#getDelay(java.util.concurrent.TimeUnit)
*/
@Override
public long getDelay(TimeUnit unit) {
// TODO Auto-generated method stubgt
long time=this.expire-System.currentTimeMillis();
return unit.convert(time, unit.MILLISECONDS);
}
}
基于delayqueue的缓存
猜你喜欢
转载自blog.csdn.net/andyzhu_2005/article/details/79996133
今日推荐
周排行