map缓存

     

有时候需要将一批数据缓存,并且按照最近最少使用的规则淘汰。这时候就应该想到LRUMap。

LRUMap是一种尺寸固定的Map,当它的容量达到最大时采用最近最少使用的规则淘汰相应的元素。


    LRUMap cache = newLRUMap(5);

    cache.put("01", "news 1");
    cache.put("02", "news 2");
    cache.put("03", "news 3");
    cache.put("04", "news 4");
    cache.put("05", "news 5");

    String news1 = (String)cache.get("01");
    String news2 = (String)cache.get("02");
    String news3 = (String)cache.get("04");
    String news4 = (String)cache.get("03");
    String news5 = (String)cache.get("02");
    String news6 = (String)cache.get("05");

    cache.put("06", "news 6");

    Iterator it = cache.entrySet().iterator();
    while(it.hasNext()){
       Map.Entry entry = (Map.Entry)it.next();
       System.out.println(entry.getValue());
    }


 



LazyMap:当你需要获取某个键值但发现该键值不存在时自动填充该键值,那就该考虑使用LazyMap,LazyMap采用了装饰模式装饰一个Map,并采用一个Transformer来决定当键值不存在时填充的规则(或者Factory)。

 


Transformer reverseString = newTransformer(){
       publicObject transform(Object object){
           String name = (String)object;
           String reverse = StringUtils.reverse(name);
           returnreverse;
       }
    };

    Map names = newHashMap();
    Map lazynames = LazyMap.decorate(names, reverseString);

    System.out.println(lazynames.get("hello"));
    System.out.println(lazynames.get("thanks"));
    System.out.println(lazynames.get("not at all"));

    Factory factory = newFactory(){
       publicObject create(){
           return"NB";
       }
    };

    Map lazyname = LazyMap.decorate(names, factory);

    System.out.println(lazyname.get("woshishei"));
    System.out.println(lazyname.get("nishishei"));
    System.out.println(lazyname.get("not at all"));


 



综合使用LRUMap和LazyMap:


Transformer reverseString = newTransformer(){
       publicObject transform(Object object){
           String name = (String)object;
           String reverse = StringUtils.reverse(name);
           returnreverse;
       }
    };

    LRUMap lruMap = newLRUMap(5);
    Map lazyMap = LazyMap.decorate(lruMap, reverseString);

    String name0 = (String)lazyMap.get("test0");
    String name1 = (String)lazyMap.get("test0");
    String name2 = (String)lazyMap.get("test0");
    String name3 = (String)lazyMap.get("test2");
    String name4 = (String)lazyMap.get("test4");
    String name5 = (String)lazyMap.get("test5");
    String name6 = (String)lazyMap.get("test0");
    String name7 = (String)lazyMap.get("test6");
    String name8 = (String)lazyMap.get("test8");
    String name9 = (String)lazyMap.get("test5");

    Iterator it = lazyMap.entrySet().iterator();
    while(it.hasNext()){
       Map.Entry entry = (Map.Entry)it.next();
       System.out.println(entry.getValue());
    }


 



当然可以在Transformer里可以做任何事,比如从网站爬取数据,并转换成相应的对象等。

如下代码从pchome的ip地址库中查询IP地址,首先定义一个ArticleTransformer对象,每次根据传入的参数从一个URL查询数据并返回。


public class ArticleTransformer implements Transformer {

	protected final static int BUF_SIZE = 300;

	protected String queryURL;
	public String getQueryURL(){return queryURL;}
	public void setQueryURL(String queryURL){this.queryURL = queryURL;}

	protected long timeOut;
	public long getTimeOut(){return timeOut;}
	/**
	 * @param timeOut 单位:ms
	 */
	public void setTimeOut(long timeOut){this.timeOut = timeOut;}

	public Object transform(Object obj){
		ArticleRetriever retriever = new ArticleRetriever((String)obj);
		try{
			Thread thread = new Thread(retriever);
			thread.start();
			thread.join(timeOut);
			//Thread.sleep(3*1000);
		}catch(InterruptedException e){
			System.out.println("url request time out.");
		}
		return retriever.getResponse();
	}

	public class ArticleRetriever implements Runnable{
		private String name;
		private String response;

		public ArticleRetriever(String name){
			this.name = name;
		}

		public String getResponse(){
			return this.response;
		}

		public void run(){

			HttpURL url = null;

			try{
				HttpClient client = new HttpClient();

				url = new HttpURL(queryURL+name);

				GetMethod getMethod = new GetMethod(url.toString());

				getMethod.getParams().setContentCharset("UTF-8");

				client.executeMethod(getMethod);

				response = getMethod.getResponseBodyAsString();
			}catch(Exception e){
				System.out.println("Error get Article  " + url);
			}

		}
	}

}
 


 



然后如下代码调用


String queryURL = "http://whois.pconline.com.cn/ip.jsp?ip=";
	ArticleTransformer transformer = new ArticleTransformer();
	transformer.setQueryURL(queryURL);
	transformer.setTimeOut(500);

	Map lruMap = new LRUMap(3);
	articleMap = LazyMap.decorate(lruMap, transformer);

	String ip1 = (String)articleMap.get("127.0.0.1");
	String ip2 = (String)articleMap.get("121.0.29.241");
	String ip3 = (String)articleMap.get("10.25.145.123");
	String ip4 = (String)articleMap.get("10.121.20.35");
	String ip5 = (String)articleMap.get("121.230.145.60");
	String ip6 = (String)articleMap.get("27.186.214.221");
	String ip7 = (String)articleMap.get("127.0.0.1");

	try {
		Writer writer = new FileWriter("D://hello.txt");

		Iterator it = articleMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			System.out.println(entry.getValue());
			if(entry.getValue() == null){
				writer.write("null\n");
			}else{
				writer.write((String)entry.getValue());
			}
		}
		if(writer!=null) writer.close();
	} catch (IOException e) {
		System.out.println("Error write file");
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值