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.數據查找過程: