Dubbo缓存
Dubbo中提供声明式缓存,用于加速热门数据的访问速度。
1.缓存策略
Dubbo提供的AbstractCacheFactory:
public abstract class AbstractCacheFactory implements CacheFactory {
private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
public Cache getCache(URL url) {
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);
}
使用ConcurrentMap
保存url对应的cache,有三种缓存策略:
- ThreadLocal
内部维护一个ThreadLocal
类型,线程的本地变量,保证线程安全。
- LRU
最近最少使用原则删除多余的缓存,保持最热的数据被缓存。
内部维护LinkedHashMap
实现LRU算法。
- JCache
与JSR107集成,桥接各种缓存实现。
2.解析Cache
Dubbo初始化指定protocol的时候,使用装饰器模式把所有需要加载的过滤器封装到目标protocol上,cache层的逻辑在这个时候就会注入到业务逻辑中去。