代码结构
ColorDatabase.java
/* * All content copyright (c) Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All * rights reserved. */package org.terracotta;import java.awt.Color;import java.util.Map;import java.util.HashMap;/** * This class simulates an external color database that is used to populate the * ColorCache. The primary, named AWT colors are stored in a map. Anytime a * color is requested and found, the calling thread is put to sleep for 3 seconds * to simulate a slow or overloaded database. */public class ColorDatabase { private static final MapcolorMap = new HashMap (); static { colorMap.put("red", Color.red); colorMap.put("blue", Color.blue); colorMap.put("green", Color.green); colorMap.put("white", Color.white); colorMap.put("black", Color.black); colorMap.put("lightGray", Color.lightGray); colorMap.put("gray", Color.gray); colorMap.put("darkGray", Color.darkGray); colorMap.put("pink", Color.pink); colorMap.put("orange", Color.orange); colorMap.put("yellow", Color.yellow); colorMap.put("magenta", Color.magenta); colorMap.put("cyan", Color.cyan); } public ColorDatabase() { } /** * Simulates retrieving expensive object from SOR. */ public Color getColor(String name) { Color color = colorMap.get(name); if(color == null) { return null; } try { Thread.sleep(3000); } catch(Exception e) {} return color; }}
ColorCache.java
/* * All content copyright (c) Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All * rights reserved. */package org.terracotta;import net.sf.ehcache.*;import java.awt.Color;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class ColorCache { private static final CacheManager cacheManager = new CacheManager(); private static final ColorDatabase colorDatabase = new ColorDatabase(); public ColorCache() { /**/ } public Color getColor(String name) { Element elem = getCache().get(name); if (elem == null) { Color color = colorDatabase.getColor(name); if (color == null) { return null; } getCache().put(elem = new Element(name, color)); } return (Color) elem.getValue(); } private Color getCachedColor(String name) { Element elem = getCache().get(name); return elem != null ? (Color) elem.getValue() : null; } public String[] getColorNames() { @SuppressWarnings("unchecked") Iteratorehcache.xml 放在类加载路径上:keys = ((List ) getCache().getKeys()).iterator(); List list = new ArrayList (); while (keys.hasNext()) { String name = keys.next(); if (getCachedColor(name) != null) { list.add(name); } } return list.toArray(new String[list.size()]); } public long getTTL() { return getCache().getCacheConfiguration().getTimeToLiveSeconds(); } public long getTTI() { return getCache().getCacheConfiguration().getTimeToIdleSeconds(); } public int getSize() { return getCache().getSize(); } private Ehcache getCache() { return cacheManager.getEhcache("colors"); }}
Test.java
package org.terracotta;import java.awt.Color;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ColorCache cache = new ColorCache(); long now = System.currentTimeMillis(); Color red=cache.getColor("red"); long elapsed = System.currentTimeMillis() - now; //第一次获取数据时间为: System.out.println("第一次获取数据时间为:"+elapsed+"---"+red); long now2 = System.currentTimeMillis(); Color red2=cache.getColor("red"); long elapsed2 = System.currentTimeMillis() - now2; //第2次获取数据时间为: System.out.println("第2次获取数据时间为:"+elapsed2+"---"+red2); }}测试结果:
第一次获取数据时间为:3003---java.awt.Color[r=255,g=0,b=0]
第2次获取数据时间为:0---java.awt.Color[r=255,g=0,b=0]