600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 一个LinkedList的简单体现(用户缓存手机通话记录)

一个LinkedList的简单体现(用户缓存手机通话记录)

时间:2021-06-24 02:46:27

相关推荐

一个LinkedList的简单体现(用户缓存手机通话记录)

public class RequestCache {

// TODO cache lifeTime

// 限制最多缓存10条

private static int CACHE_LIMIT = 10;

@SuppressWarnings("unchecked")

private LinkedList history;

private Hashtable<String, String> cache;

@SuppressWarnings("unchecked")

public RequestCache(){

history = new LinkedList();

cache = new Hashtable<String, String>();

}

@SuppressWarnings("unchecked")

public void put(String url, String data){

history.add(url);

// too much in the cache, we need to clear something

//如果缓存中超过规定的条数,需要删除一部分

//这里需要注意一下“链表是如何删除数据的”:pool()表示获取并移除此列表的头

if(history.size() > CACHE_LIMIT){

String old_url = (String) history.poll();

cache.remove(old_url);

}

cache.put(url, data);

}

public String get(String url){

return cache.get(url);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。