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(); } }
相关推荐
apache开源项目源码ibatis-3-core-src-3.0.0.227 ibatis框架java源程序 spring,struts,hibernate,ibatis,框架源码 各种ibatis框架应用源码,你会从中得到意想不到的效果! apache开源组织开发的开源项目源码,其...
ibatis缓存介绍 - 勇泽 - 博客园ibatis缓存介绍 - 勇泽 - 博客园ibatis缓存介绍 - 勇泽 - 博客园ibatis缓存介绍 - 勇泽 - 博客园
ibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zip
ibatis-3-core-3.0.0.242.zip ibatis-3-core-3.0.0.242.zip ibatis-3-core-3.0.0.242.zip ibatis-3-core-3.0.0.242.zip
ibatis-3-core-3.0.0.200
iBATIS-SqlMaps-2-Tutorial_cniBATIS-SqlMaps-2-Tutorial_cn.pdf.pdfiBATIS-SqlMaps-2-Tutorial_cn.pdfiBATIS-SqlMaps-2-Tutorial_cn.pdf
Ibatis基本配置---[环境搭建
ibatis-3-core-3.0.0.204 最新官方下载版
ibatic框架最新驱动--------------------
ibatis 开发指南 和 iBATIS-SqlMaps两本图书
iBATIS框架源码剖析-任钢
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Account"> <typeAlias alias="Account" type="Account"/> ...
ibatis2源码与分析
iBATIS-3-User-Guide.pdf what is iBATIS? ...........
iBATIS缓存的使用方法
ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网
flex 整合 spring+struts2+ibatis - 乐的日志 - 网易博客.htm flex 整合 spring+struts2+ibatis - 乐的日志 - 网易博客.htm
ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包
ibatis的原码 ibatis源码 ibatis源码 ibatis源码
iBATIS缓存介绍 iBATIS二级缓存 iBATIS缓存配置