ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ package org.apache.ibatis.cache.decorators; import java.util.Deque; import java.util.LinkedList; import org.apache.ibatis.cache.Cache; /** * FIFO (first in, first out) cache decorator. * * @author Clinton Begin */ public class FifoCache implements Cache { //缓存实体 private final Cache delegate; //key列表 private final Deque<Object> keyList; //长度 private int size; public FifoCache(Cache delegate) { this.delegate = delegate; this.keyList = new LinkedList<>(); //长度设置为1024 this.size = 1024; } @Override public String getId() { return delegate.getId(); } @Override public int getSize() { return delegate.getSize(); } public void setSize(int size) { this.size = size; } @Override public void putObject(Object key, Object value) { cycleKeyList(key); delegate.putObject(key, value); } @Override public Object getObject(Object key) { return delegate.getObject(key); } @Override public Object removeObject(Object key) { return delegate.removeObject(key); } @Override public void clear() { delegate.clear(); keyList.clear(); } private void cycleKeyList(Object key) { //在末尾增加数据 keyList.addLast(key); if (keyList.size() > size) { //在头部移除数据 Object oldestKey = keyList.removeFirst(); delegate.removeObject(oldestKey); } } } ~~~