💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ 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); } } } ~~~