博学之,审问之,慎思之,明辨之,笃行之!

导航

【Mybatis】LRU缓冲实现

LRU (least recently used) 即“最近最少使用”缓冲淘汰算法。
Mybatis 的 LRUCache 是利用 java 的基础工具包 LinkedHashMap 特性二次开发完成。

Cache 接口定义

public interface Cache {

  /**
   * 缓冲 ID
   */
  String getId();

  /**
   * 存储数据
   */
  void putObject(Object key, Object value);

  /**
   * 获取数据
   */
  Object getObject(Object key);

  /**
   * 删除数据
   */
  Object removeObject(Object key);

  /**
   * 清空缓冲
   */
  void clear();

  /**
   * 获取缓冲大小
   */
  int getSize();
  
  ReadWriteLock getReadWriteLock();
}

LRUCache 实现类

LRUCache 实现 Cache 接口

package org.apache.ibatis.cache.decorators;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import org.apache.ibatis.cache.Cache;

/**
 * Lru (least recently used) cache decorator
 */
public class LruCache implements Cache {

  private final Cache delegate;
  private Map<Object, Object> keyMap;
  private Object eldestKey;

  public LruCache(Cache delegate) {
    this.delegate = delegate;
    setSize(1024);
  }

  @Override
  public String getId() {
    return delegate.getId();
  }

  @Override
  public int getSize() {
    return delegate.getSize();
  }

  public void setSize(final int size) {
    keyMap = new LinkedHashMap<Object, Object>(size, .75F, true) {
      private static final long serialVersionUID = 4267176411845948333L;

      @Override
      protected boolean removeEldestEntry(Map.Entry<Object, Object> eldest) {
        boolean tooBig = size() > size;
        if (tooBig) {
          eldestKey = eldest.getKey();
        }
        return tooBig;
      }
    };
  }

  @Override
  public void putObject(Object key, Object value) {
    delegate.putObject(key, value);
    cycleKeyList(key);
  }

  @Override
  public Object getObject(Object key) {
    keyMap.get(key); //touch
    return delegate.getObject(key);
  }

  @Override
  public Object removeObject(Object key) {
    return delegate.removeObject(key);
  }

  @Override
  public void clear() {
    delegate.clear();
    keyMap.clear();
  }

  @Override
  public ReadWriteLock getReadWriteLock() {
    return null;
  }

  private void cycleKeyList(Object key) {
    keyMap.put(key, key);
    if (eldestKey != null) {
      delegate.removeObject(eldestKey);
      eldestKey = null;
    }
  }
}

posted on 2020-12-30 14:46  PP杰  阅读(229)  评论(0编辑  收藏  举报