- 浏览: 958569 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
SqlSessionFactory初始化:http://donald-draper.iteye.com/admin/blogs/2331917
Mybatis加载解析Mapper(xml)文件第一讲:http://donald-draper.iteye.com/blog/2333125
Mybatis加载解析Mapper(xml)文件第二讲:http://donald-draper.iteye.com/blog/2333191
Mybatis 解析Mapper(class):http://donald-draper.iteye.com/blog/2333293
Mybatis的Environment解析详解:http://donald-draper.iteye.com/admin/blogs/2334133
mybatis 动态标签语句的解析(BoundSql):http://donald-draper.iteye.com/admin/blogs/2334135
在前面将Mybatis解析文件,SQLsession更新,查询,常提到Cache,今天我们来看一下
从Cache接口,可以看出Cache,有id,size,ReadWriteLock属性,put,get,remove Obejct操作;
再来看一下Mybatis默认的缓存PerpetualCache
从PerpetualCache,我们可以看到,PerpetualCache的实现其实是通过Map,put,get,remove,clear操作复用Map相应操作。
再看LruCache
总结:
Mybatis缓存的实现是通过Map来实现,缓存的新增,获取,移除对象,都是通过Map的相关操作;在LRUCache中,内部有一个缓存代理,LRU的新增,获取,移除对象,是通过缓存代理相关的操作,LRU中还有两个元素,为keyMap和eldestKey,keyMap为缓存中对应的key,keyMap
线程安全的LinkedHashMap,同时,重写了LinkedHashMap的removeEldestEntry,removeEldestEntry方法的含义是,当项Map中添加对象时,是否会需要移除eldest java.util.Map.Entry 对象,KeyMap添加对象后,size大于初始化容量,则从KeyMap中移除对应的key,同时将key赋值于eldestKey,当我们向LRUCache中添加对象时,同时将key添加KeyMap,并处罚是否超出容量检查,如果超出,则从代理缓存中移除对应的对象。
Mybatis加载解析Mapper(xml)文件第一讲:http://donald-draper.iteye.com/blog/2333125
Mybatis加载解析Mapper(xml)文件第二讲:http://donald-draper.iteye.com/blog/2333191
Mybatis 解析Mapper(class):http://donald-draper.iteye.com/blog/2333293
Mybatis的Environment解析详解:http://donald-draper.iteye.com/admin/blogs/2334133
mybatis 动态标签语句的解析(BoundSql):http://donald-draper.iteye.com/admin/blogs/2334135
在前面将Mybatis解析文件,SQLsession更新,查询,常提到Cache,今天我们来看一下
package org.apache.ibatis.cache; import java.util.concurrent.locks.ReadWriteLock; public interface Cache { public abstract String getId(); public abstract int getSize(); public abstract void putObject(Object obj, Object obj1); public abstract Object getObject(Object obj); public abstract Object removeObject(Object obj); public abstract void clear(); public abstract ReadWriteLock getReadWriteLock(); }
从Cache接口,可以看出Cache,有id,size,ReadWriteLock属性,put,get,remove Obejct操作;
再来看一下Mybatis默认的缓存PerpetualCache
public class PerpetualCache implements Cache { private String id; private Map cache; private ReadWriteLock readWriteLock; public PerpetualCache(String id) { cache = new HashMap(); readWriteLock = new ReentrantReadWriteLock(); this.id = id; } public String getId() { return id; } public int getSize() { return cache.size(); } public void putObject(Object key, Object value) { cache.put(key, value); } public Object getObject(Object key) { return cache.get(key); } public Object removeObject(Object key) { return cache.remove(key); } public void clear() { cache.clear(); } public ReadWriteLock getReadWriteLock() { return readWriteLock; } public boolean equals(Object o) { if(getId() == null) throw new CacheException("Cache instances require an ID."); if(this == o) return true; if(!(o instanceof Cache)) { return false; } else { Cache otherCache = (Cache)o; return getId().equals(otherCache.getId()); } } public int hashCode() { if(getId() == null) throw new CacheException("Cache instances require an ID."); else return getId().hashCode(); } }
从PerpetualCache,我们可以看到,PerpetualCache的实现其实是通过Map,put,get,remove,clear操作复用Map相应操作。
再看LruCache
public class LruCache implements Cache { private final Cache _flddelegate;//缓存代理 private Map keyMap;//key Map private Object eldestKey;//最老的key public LruCache(Cache delegate) { _flddelegate = delegate; setSize(1024); } public String getId() { return _flddelegate.getId(); } public int getSize() { return _flddelegate.getSize(); } //设置缓存大小 public void setSize(final int size) { //获取线程线程安全的LinkedHashMap集合 keyMap = Collections.synchronizedMap(new LinkedHashMap(0.75F, true, size) { //同时重写removeEldestEntry,当这个方法返回为true是,则移除eldest protected boolean removeEldestEntry(java.util.Map.Entry eldest) { boolean tooBig = size() > size; if(tooBig) //如果Map目前的size大于初始化Map大小,则将最老key赋给eldestKey eldestKey = eldest.getKey(); return tooBig; } private static final long serialVersionUID = 4267176411845948333L; final int val$size; final LruCache this$0; { this$0 = LruCache.this; size = i; super(x0, x1, x2); } }); } //将KV对放入缓存 public void putObject(Object key, Object value) { _flddelegate.putObject(key, value); //并将key放入LRUCache的Key Map中 cycleKeyList(key); } public Object getObject(Object key) { keyMap.get(key); return _flddelegate.getObject(key); } public Object removeObject(Object key) { return _flddelegate.removeObject(key); } public void clear() { _flddelegate.clear(); //在清除缓存的时候同时,清除Key Map keyMap.clear(); } public ReadWriteLock getReadWriteLock() { return _flddelegate.getReadWriteLock(); } //如果Key Map,已达到负载上限,则从缓存中移除eldestKey private void cycleKeyList(Object key) { keyMap.put(key, key); if(eldestKey != null) { _flddelegate.removeObject(eldestKey); eldestKey = null; } } }
总结:
Mybatis缓存的实现是通过Map来实现,缓存的新增,获取,移除对象,都是通过Map的相关操作;在LRUCache中,内部有一个缓存代理,LRU的新增,获取,移除对象,是通过缓存代理相关的操作,LRU中还有两个元素,为keyMap和eldestKey,keyMap为缓存中对应的key,keyMap
线程安全的LinkedHashMap,同时,重写了LinkedHashMap的removeEldestEntry,removeEldestEntry方法的含义是,当项Map中添加对象时,是否会需要移除eldest java.util.Map.Entry 对象,KeyMap添加对象后,size大于初始化容量,则从KeyMap中移除对应的key,同时将key赋值于eldestKey,当我们向LRUCache中添加对象时,同时将key添加KeyMap,并处罚是否超出容量检查,如果超出,则从代理缓存中移除对应的对象。
//LinkedHashMap /** * Returns <tt>true</tt> if this map should remove its eldest entry. * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after * inserting a new entry into the map. It provides the implementor * with the opportunity to remove the eldest entry each time a new one * is added. This is useful if the map represents a cache: it allows * the map to reduce memory consumption by deleting stale entries. * * <p>Sample use: this override will allow the map to grow up to 100 * entries and then delete the eldest entry each time a new entry is * added, maintaining a steady state of 100 entries. * <pre> * private static final int MAX_ENTRIES = 100; * * protected boolean removeEldestEntry(Map.Entry eldest) { * return size() > MAX_ENTRIES; * } * </pre> * * <p>This method typically does not modify the map in any way, * instead allowing the map to modify itself as directed by its * return value. It <i>is</i> permitted for this method to modify * the map directly, but if it does so, it <i>must</i> return * <tt>false</tt> (indicating that the map should not attempt any * further modification). The effects of returning <tt>true</tt> * after modifying the map from within this method are unspecified. * * <p>This implementation merely returns <tt>false</tt> (so that this * map acts like a normal map - the eldest element is never removed). * * @param eldest The least recently inserted entry in the map, or if * this is an access-ordered map, the least recently accessed * entry. This is the entry that will be removed it this * method returns <tt>true</tt>. If the map was empty prior * to the <tt>put</tt> or <tt>putAll</tt> invocation resulting * in this invocation, this will be the entry that was just * inserted; in other words, if the map contains a single * entry, the eldest entry is also the newest. * @return <tt>true</tt> if the eldest entry should be removed * from the map; <tt>false</tt> if it should be retained. */ protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return false; }
//Collections /** * Returns a synchronized (thread-safe) map backed by the specified * map. In order to guarantee serial access, it is critical that * [b]all[/b] access to the backing map is accomplished * through the returned map.<p> * * It is imperative that the user manually synchronize on the returned * map when iterating over any of its collection views: * <pre> * Map m = Collections.synchronizedMap(new HashMap()); * ... * Set s = m.keySet(); // Needn't be in synchronized block * ... * synchronized (m) { // Synchronizing on m, not s! * Iterator i = s.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> * Failure to follow this advice may result in non-deterministic behavior. * * <p>The returned map will be serializable if the specified map is * serializable. * * @param m the map to be "wrapped" in a synchronized map. * @return a synchronized view of the specified map. */ public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) { return new SynchronizedMap<>(m); }
发表评论
-
DefaultSqlSession第三讲-事务提交,回滚,关闭SqlSession,清除缓存
2016-11-20 11:07 5610上面两篇讲过query和update及flushStateme ... -
DefaultSqlSession第二讲-更新,刷新Statement
2016-11-20 11:06 595上一篇文章中,我们讲到DefaultSqlSession的查询 ... -
DefaultSqlSession第一讲query解析
2016-11-20 11:06 1578上一篇文章:我们说过DefaultSqlSession,Def ... -
Mybatis的SqlSession解析
2016-11-20 11:02 2454在前文中,Mybatis使用教程中,有下面一段代码: Sql ... -
Mybatis的Reflector解析
2016-11-18 12:53 2127Mybatis的MetaObject解析:http://don ... -
Mybatis的MetaObject解析
2016-11-18 11:40 9518SqlSessionFactory初始化:http://don ... -
mybatis 动态标签语句的解析(BoundSql)
2016-10-30 09:48 5676SqlSessionFactory初始化:http://don ... -
Mybatis的Environment解析详解
2016-10-29 18:28 1958SqlSessionFactory初始化:http://don ... -
Mybatis 解析Mapper(class)
2016-10-26 11:44 3273SqlSessionFactory初始化:http://don ... -
Mybatis加载解析Mapper(xml)文件第二讲
2016-10-25 21:30 4702SqlSessionFactory初始化:http://don ... -
Mybatis加载解析Mapper(xml)文件第一讲
2016-10-25 16:51 6093SqlSessionFactory初始化:http://don ... -
SqlSessionFactory初始化
2016-10-20 15:38 2413mybatis 使用教程:http://donald-drap ... -
mybatis 使用教程
2016-10-14 09:03 872Myeclispe下mybatis generator的使 ... -
Spring+Mybatis多数据源的实现
2016-09-21 18:15 3056浅谈Spring事务隔离级别:http://www.cnblo ... -
mybaitis CDATA 防止字符被解析
2016-08-17 18:45 693在使用mybatis 时我们sql是写在xml 映射文件中,如 ... -
Myeclispe下mybatis generator的使用
2016-07-05 18:05 1338准备:下载附件包解压到myeclispe的dropins文件夹 ...
相关推荐
主要介绍了MyBatis缓存实现原理及代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
通过log4j打印来查看mybatis缓存实现的机制, 原博客http://blog.csdn.net/zouxucong/article/details/68947052
springmvc整合Mybatis,Redis;实现将查询的数据进行二级缓存处理
mybatis二级缓存 + reads做第三级缓存
介绍:一个很简单的Maven项目,对Mybatis的缓存进行测试
学习MyBatis框架的一级缓存和二级缓存,明确缓存的工作机制,并实现MyBatis框架与第三方缓存EhCache的整合。
MyBatis 二级缓存 关联刷新实现1、MyBatis缓存介绍2、二级缓存问题2.1、数据不一致问题验证2.2、问题处理思路3、关联缓存刷新实现 1、MyBatis缓存介绍 Mybatis提供对缓存的支持,但是在没有配置的默认情况下,它只...
主要介绍了Mybatis-plus基于redis实现二级缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了SpringBoot下Mybatis的缓存的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring+mybatis+redis,mybatis作为缓存,仅仅是入门的配置,我找了好多的教程才最终配置好,资源绝对没问题。请放心下载
redis集成 mybatis缓存实现和redis session共享问题,详细代码。 结合项目中优点: (1) 因为数据存在内存中,类似于 HashMap ,HashMap 的优势就是查找和操作的时间复杂度都是O (1) 。 (2) Redis 本质上是一个 Key-...
Mybatis延迟加载和缓存
本项目是基于maven.且需要本地安装了redis之后才能使用,因为项目里面不仅集成了redis,而且里面还集成了shiro。这是从我自己的eclipse中直接copy出来的。实测可用
ehcache是一个快速内存缓存框架,java项目里用起来很方便,本来想实现分布式缓存的,但是实践下来redis更适合分布式,所以这里面把分布缓存的配置注释掉了
解读: 1、客户从数据库获取数据视...5、由于mybatis的缓存只用了map实现,所以mybatis允许缓存由第三方缓存来实现,并定义了cache接口,第三方只要实现该接口即可,和mybatis整合在一起后由mybatis在程序中进行调用;
mybatis教程分为10部分: 1.mybatis快速入门 2.mybatis CRUD 3.mybatis注解实现 4.mybatis相关优化 5.mybatis解决字段名与实体类属性名不相同的冲突 ...9.mybatis缓存(一级缓存、二级缓存) 10.spring集成mybatis
整合EhCache,对Mybatis的二级缓存进行管理和对spring进行缓存管理 整合FastJson对指定http类型的数据进行转换 整合hibernate.validator校验器对controller接口参数进行校验 使用了springmvc统一异常处理 使用了...
答:MyBatis的缓存分为一级缓存和二级缓存,一级缓存放在session里面,默认就有,二级缓存放在它的命名空间里,默认是不打开的,使用二级缓存属性类需要实现Serializable序列化接口(可用来保存对象的状态),可在它的映射...
该资源为apache ignite实现mybatis二级缓存所需要的jar包,需要的可以下载。
springboot+mybatis+druid+redis实现数据库读写分离和缓存