疯狂java迷

 

一个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);
 }
}

posted on 2011-11-30 17:09  疯狂java迷  阅读(312)  评论(0编辑  收藏  举报

导航