首页 > 其他分享 >ibatis-FifoCache

ibatis-FifoCache

时间:2024-05-15 14:53:12浏览次数:23  
标签:LinkedList FifoCache ibatis delegate key keyList size

核心代码

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

}

 

标签:LinkedList,FifoCache,ibatis,delegate,key,keyList,size
From: https://www.cnblogs.com/use-D/p/18152926

相关文章