ibatis-FifoCache
核心代码
Deque<Object> keyList = new LinkedList<>();
为什么使用LinkedList?单向链表。
使用LinkedList实现FIFO,支持头、尾节点的单向链表。
添加时,判断数量大于初始化值时,删除头结点。
源码:
public class FifoCache implements Cache { private final Cache delegate; private final Deque<Object> keyList; private int size; public FifoCache(Cache delegate) { this.delegate = delegate; this.keyList = new LinkedList<>(); this.size = 1024; } @Override public void putObject(Object key, Object value) { cycleKeyList(key); delegate.putObject(key, value); }private void cycleKeyList(Object key) { keyList.addLast(key); if (keyList.size() > size) { Object oldestKey = keyList.removeFirst(); delegate.removeObject(oldestKey); } } }
收藏文章数量从多到少与“把书读薄”是一个道理