abstractCaheFactory是一个抽象类,统一了获取缓存对象的过程。
private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();//线程安全性考虑用concurrentMap,用jvm缓存来缓存缓存对象。
//缓存工厂,用来根据资源URL,invocation获取缓存对象,创建缓存对象的过程交给子类实现。
@Override
public Cache getCache(URL url, Invocation invocation) {//相同的用魔板方法固定起来
url = url.addParameter(Constants.METHOD_KEY, invocation.getMethodName());
String key = url.toFullString();
Cache cache = caches.get(key);
if (cache == null) {
caches.put(key, createCache(url));
cache = caches.get(key);
}
return cache;
}
protected abstract Cache createCache(URL url);//各个子类具体实现不同的地方,方法抽象起来