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; } } }