有时候需要将一批数据缓存,并且按照最近最少使用的规则淘汰。这时候就应该想到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); } } } } <SPAN style="DISPLAY: none"> </SPAN> |
然后如下代码调用
代码如下 | 复制代码 |
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("nulln"); }else{ writer.write((String)entry.getValue()); } } if(writer!=null) writer.close(); } catch (IOException e) { System.out.println("Error write file"); }
|
最后输出如下的结果:
江苏省泰州市(泰兴)电信
河北省保定市电信
Internet保留地址
时间: 2024-11-09 09:26:52