zoukankan      html  css  js  c++  java
  • 【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;
        }
      }
    }
    
  • 相关阅读:
    蘑菇街电话面试记录
    阿里校招在线笔试
    前端小记
    校招记录
    leetcode部分解题思路
    搜的面试题零散记录
    网页小记录
    开机进入GRUB不要慌,命令行也可启动Linux
    Error running Tomcat 6: Address localhost:8080 is already in use
    IDEA 自动提示快捷键,类似VS Ctrl + J
  • 原文地址:https://www.cnblogs.com/newber/p/14211402.html
Copyright © 2011-2022 走看看