zoukankan      html  css  js  c++  java
  • 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();
      }
    
    }
    

      

  • 相关阅读:
    sphinx 源码阅读之分词,压缩索引,倒排——单词对应的文档ID列表本质和lucene无异 也是外部排序再压缩 解压的时候需要全部扫描doc_ids列表偏移量相加获得最终的文档ID
    详细说明XML分解(两)—DOM4J
    JSP简单的练习-用户登记表
    设计师给了px显着的单位,Android要设置多少开发商dip、dp、sp?
    左右xcode的重构选项的一些理解
    unicode下一个,读取数据库乱码问题
    java中间==、equals和hashCode差额
    MIPS台OpenWrt在系统内的路由器Rust应用程序开发
    Android采取async框架文件上传
    ios-上拉电阻负载许多其他接口
  • 原文地址:https://www.cnblogs.com/wei-zw/p/8797769.html
Copyright © 2011-2022 走看看