SoftCache 和 WeakCache的类图以下java
看一下SoftCache的源码apache
package org.apache.ibatis.cache.decorators; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.util.Deque; import java.util.LinkedList; import java.util.concurrent.locks.ReadWriteLock; import org.apache.ibatis.cache.Cache; /** * Soft Reference cache decorator * Thanks to Dr. Heinz Kabutz for his guidance here. * * @author Clinton Begin */ public class SoftCache implements Cache { // 在SoftCache 中,最近使用的一部分缓存项不会被GC回收,这就是经过将其value添加到 // hardLinksToAvoidGarbageCollection集合中实现的 private final Deque<Object> hardLinksToAvoidGarbageCollection; // 引用队列,用于记录GC回收的缓存项所对应的SoftEntry对象 private final ReferenceQueue<Object> queueOfGarbageCollectedEntries; // 底层被修饰的Cache 对象 private final Cache delegate; // 强连接的个数,默认是256 private int numberOfHardLinks; public SoftCache(Cache delegate) { this.delegate = delegate; this.numberOfHardLinks = 256; this.hardLinksToAvoidGarbageCollection = new LinkedList<Object>(); this.queueOfGarbageCollectedEntries = new ReferenceQueue<Object>(); } @Override public String getId() { return delegate.getId(); } @Override public int getSize() { removeGarbageCollectedItems(); return delegate.getSize(); } public void setSize(int size) { this.numberOfHardLinks = size; } @Override public void putObject(Object key, Object value) { // 清除被GC回收的缓存项 removeGarbageCollectedItems(); // 向缓存中添加缓存项 delegate.putObject(key, new SoftEntry(key, value, queueOfGarbageCollectedEntries)); } @Override public Object getObject(Object key) { Object result = null; // 查找对应的缓存项 @SuppressWarnings("unchecked") // assumed delegate cache is totally managed by this cache SoftReference<Object> softReference = (SoftReference<Object>) delegate.getObject(key); if (softReference != null) { result = softReference.get(); // 已经被GC 回收 if (result == null) { // 从缓存中清除对应的缓存项 delegate.removeObject(key); } else { // See #586 (and #335) modifications need more than a read lock synchronized (hardLinksToAvoidGarbageCollection) { hardLinksToAvoidGarbageCollection.addFirst(result); if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) { hardLinksToAvoidGarbageCollection.removeLast(); } } } } return result; } @Override public Object removeObject(Object key) { removeGarbageCollectedItems(); return delegate.removeObject(key); } @Override public void clear() { synchronized (hardLinksToAvoidGarbageCollection) { // 清理强引用集合 hardLinksToAvoidGarbageCollection.clear(); } // 清理被GC回收的缓存项 removeGarbageCollectedItems(); delegate.clear(); } @Override public ReadWriteLock getReadWriteLock() { return null; } private void removeGarbageCollectedItems() { SoftEntry sv; // 遍历集合 while ((sv = (SoftEntry) queueOfGarbageCollectedEntries.poll()) != null) { // 将value对象清除 delegate.removeObject(sv.key); } } /** **SoftCache 中缓存项的value是SoftEntry对象,SoftEnty对象继承了SoftReference **其中指向key的引用是强引用,而指向value的引用是若引用 **/ private static class SoftEntry extends SoftReference<Object> { private final Object key; SoftEntry(Object key, Object value, ReferenceQueue<Object> garbageCollectionQueue) { super(value, garbageCollectionQueue); this.key = key; } } }