1.缓存java
[1]SQL语句或查询条件不一样面试
[2]分属不一样SqlSession对象sql
[3]查询前执行clearCache()数据库
[4]提交事务apache
2.一级缓存api
3.二级缓存缓存
跟Web应用中application对象做用范围相似。mybatis
<!-- 启用二级缓存 -->app <setting name="cacheEnabled" value="true"/>框架 |
<cache eviction="FIFO" flushInterval="60000"readOnly="true" size="512"/>
4.外置二级缓存组件
EnhancerByCGLIBEnhancerByCGLIB
eb28a8d2①为何要整合EHCache?
②操做
[1]添加jar包
[2]加入EHCache自身配置文件ehcache.xml
<ehcache> <!--设置硬盘缓存目录:内存中存储不下就能够往这个目录下存储了。会自动生成数据文件--> <diskStore path="D:/temp"/> <defaultCache<!--默认的缓存配置--> maxElementsInMemory="10000" maxElementsOnDisk="10000000" eternal="false" timeToIdleSeconds="20" timeToLiveSeconds="120" overflowToDisk="true" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache> |
属性说明: * diskStore:指定数据在磁盘中的存储位置。 * defaultCache:当借助CacheManager.add("demoCache")建立Cache时,EhCache便会采用<defalutCache/>指定的的管理策略
如下属性是必须的: * maxElementsInMemory - 在内存中缓存的element的最大数目 * maxElementsOnDisk - 在磁盘上缓存的element的最大数目,如果0表示无穷大 * eternal - 设定缓存的elements是否永远不过时。若是为true,则缓存的数据始终有效,若是为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断 * overflowToDisk - 设定当内存缓存溢出的时候是否将过时的element缓存到磁盘上
如下属性是可选的: * timeToIdleSeconds - 当缓存在EhCache中的数据先后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大 * timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大 diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每一个Cache都应该有本身的一个缓冲区. * diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。 * diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每隔120s,相应的线程会进行一次EhCache中数据的清理工做 * memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出) |
[3]开启EHCache缓存XxxMapper.xml文件中配置
[4]测试
openSession = sqlSessionFactory.openSession(); CustomerMapper mapper = openSession.getMapper(CustomerMapper.class);
System.out.println(customer.getCustName()); customer = mapper.getCustomerByCustId(2); System.out.println(customer.getCustName());
openSession.close();
openSession = sqlSessionFactory.openSession(); mapper = openSession.getMapper(CustomerMapper.class);
customer = mapper.getCustomerByCustId(1); System.out.println(customer.getCustName()); |
5.二级缓存的原理:
6.二级缓存失效状况
[1]在查询select标签内设置useCache="false"
<selectid="getStuById" parameterType="Integer"resultType="Student" useCache="false">
selectstu_id stuId,stu_name stuName from tbl_student where stu_id=#{stuId}
</select>
[2]在执行增删改操做时刷新缓存
<updateid="updateStu"parameterType="com.atguigu.mybatis.entity.Student" flushCache="true">
updatetbl_student set stu_name=#{stuName} where stu_id=#{stuId}
</update>
这是默认设置,一般没必要修改
7.缓存命中率
AAA [DEBUG] 2017-04-08 10:57:04,342(1036) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.5 AAA [DEBUG] 2017-04-08 10:57:04,342(1036) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.6 AAA [DEBUG] 2017-04-08 10:57:04,343(1037) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.6666666666666666 AAA [DEBUG] 2017-04-08 10:57:04,343(1037) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.7142857142857143 AAA [DEBUG] 2017-04-08 10:57:04,343(1037) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.75 |
8.数据查找过程: