iBatis缓存实现源码分析-FIFO,LUR实现方法

  iBatis的二级缓存支持FIFO,LRU,MEMORY,OSCACHE; 从源码去分析这些缓存是如何实现的

  • FIFO
/**
 *先进先出缓存控制器
 * FIFO (first in, first out) cache controller implementation
 */
public class FifoCacheController implements CacheController {
  //缓存大小
  private int cacheSize;
  //缓存
  private Map cache;
  //缓存中key的列表
  private List keyList;

  /**
   * 默认构造器
   */
  public FifoCacheController() {
    // 默认缓存大小为100
    this.cacheSize = 100;
	//初始化一个线程安全的缓存
    this.cache = Collections.synchronizedMap(new HashMap());
	//初始化一个线程安全的key列表
    this.keyList = Collections.synchronizedList(new LinkedList());
  }

  public int getCacheSize() {
    return cacheSize;
  }

  public void setCacheSize(int cacheSize) {
    this.cacheSize = cacheSize;
  }

  /**
   * Configures the cache
   *
   * @param props Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]
   */
  public void setProperties(Properties props) {
    String size = props.getProperty("cache-size");
    if (size == null) {
      size = props.getProperty("size");
    }
    if (size != null) {
      cacheSize = Integer.parseInt(size);
    }
  }

  /**
   * 添加一个对象到缓存中
   *
   * @param cacheModel The cacheModel
   * @param key        The key of the object to be cached
   * @param value      The object to be cached
   */
  public void putObject(CacheModel cacheModel, Object key, Object value) {
	//添加对象到缓存
    cache.put(key, value);
	//将key添加到列表中
    keyList.add(key);
	// 如果key列表的长度大于缓存长度
    if (keyList.size() > cacheSize) {
      try {
		//删除表头
        Object oldestKey = keyList.remove(0);
		//同时清除该缓存数据
        cache.remove(oldestKey);
      } catch (IndexOutOfBoundsException e) {
        //ignore
      }
    }
  }

  /**
   * 从缓存中获取对象
   *
   * @param cacheModel The cache model
   * @param key        The key of the object to be returned
   * @return The cached object (or null)
   */
  public Object getObject(CacheModel cacheModel, Object key) {
    return cache.get(key);
  }

  /**
   * 删除缓存对象
   **/
  public Object removeObject(CacheModel cacheModel, Object key) {
    keyList.remove(key);
    return cache.remove(key);
  }

  /**
   * 刷新缓存清除所有数据
   *
   * @param cacheModel The cache model
   */
  public void flush(CacheModel cacheModel) {
    cache.clear();
    keyList.clear();
  }

}
  •  LRU
/**
 *最少使用缓存控制器
 * LRU (least recently used) cache controller implementation
 */
public class LruCacheController implements CacheController {
  //缓存大小
  private int cacheSize;
  //缓存
  private Map cache;
  //缓存 key的列表
  private List keyList;

  /**
   * 默认构造器
   */
  public LruCacheController() {
   // 初始化
    this.cacheSize = 100;
    this.cache = Collections.synchronizedMap(new HashMap());
    this.keyList = Collections.synchronizedList(new LinkedList());
  }
// 获取缓存大小
  public int getCacheSize() {
    return cacheSize;
  }
// 设置缓存的大小
  public void setCacheSize(int cacheSize) {
    this.cacheSize = cacheSize;
  }

  /**
   * 配置缓存
   *
   * @param props Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]
   */
  public void setProperties(Properties props) {
    String size = props.getProperty("cache-size");
    if (size == null) {
      size = props.getProperty("size");
    }
    if (size != null) {
      cacheSize = Integer.parseInt(size);
    }
  }

  /**
   * 将一个对象添加到缓存中
   *
   * @param cacheModel The cacheModel
   * @param key        The key of the object to be cached
   * @param value      The object to be cached
   */
  public void putObject(CacheModel cacheModel, Object key, Object value) {
    cache.put(key, value);
	//将key添加到队列的尾部
    keyList.add(key);
	// 如果缓存大小查过限制则删除表头的key,以及清空该缓存值
    if (keyList.size() > cacheSize) {
      try {
        Object oldestKey = keyList.remove(0);
        cache.remove(oldestKey);
      } catch (IndexOutOfBoundsException e) {
        //ignore
      }
    }
  }

  /**
   * 从缓存中获取一个对象
   *
   * @param cacheModel The cache model
   * @param key        The key of the object to be returned
   * @return The cached object (or null)
   */
  public Object getObject(CacheModel cacheModel, Object key) {
   //获取缓存数据
    Object result = cache.get(key);
	//将该key从对列中删除后再追加到尾部
    keyList.remove(key);
    if (result != null) {
      keyList.add(key);
    }
    return result;
  }
  // 删除缓存
  public Object removeObject(CacheModel cacheModel, Object key) {
    keyList.remove(key);
    return cache.remove(key);
  }

  /**
   * 清空缓存
   *
   * @param cacheModel The cache model
   */
  public void flush(CacheModel cacheModel) {
    cache.clear();
    keyList.clear();
  }

}

猜你喜欢

转载自wujiu.iteye.com/blog/2217973