Do you really know about Mybatis caching?

Mybatis cache

1) Level 1 cache : (local cache):
* sqlSession level cache. The first level cache is always open; a Map
* at the SqlSession level, the data queried during the same session with the database will be placed in the local cache.
* If you later need to get the same data, take directly from the cache, no need to go to query the database;
*
* a cache miss situation (not to use the current cache, the effect is also need to issue queries the database again ):
* 1, sqlSession is different.
* 2. The sqlSession is the same, but the query conditions are different. (The data is not in the current level 1 cache)
* 3. The sqlSession is the same, and additions, deletions, and modifications are performed between the two queries (this addition, deletion, and modification may affect the current data)
* 4. Same as sqlSession, the first level cache is manually cleared (cache empty)

2) Level 2 cache : (global cache): Cache based on the namespace level: a namespace corresponds to a level 2 cache:

  • Working Mechanism:

    • 1. For a session, query a piece of data, and this data will be placed in the first-level cache of the current session;

    • 2. If the session is closed; the data in the first-level cache will be saved in the second-level cache; for new session query information, you can refer to the content in the second-level cache;

    • 3、sqlSession=EmployeeMapper>
      The data detected by Employee in different namespaces will be placed in its corresponding cache (map)
      Effect: the data will be obtained from the second-level cache, and the detected data will be placed in the first-level cache by default. Only after the session is submitted or closed, the data in the first-level cache will be transferred to the second-level cache for
      use:

      1)、开启全局二级缓存配置:
      
    <settings>
    		<!--显式的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->
    		<setting name="cacheEnabled" value="true"/>
    </settings>
    
    	2)、去mapper.xml中配置使用二级缓存:
    
    <mapper namespace="com.atguigu.mybatis.dao.EmployeeMapper">
        
    	<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
    	<!-- <cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024"></cache> -->
    	<!--  
    	eviction:缓存的回收策略:
    		• LRU – 最近最少使用的:移除最长时间不被使用的对象。
    		• FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
    		• SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
    		• WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
    		• 默认的是 LRU。
    	flushInterval:缓存刷新间隔
    		缓存多长时间清空一次,默认不清空,设置一个毫秒值
    	readOnly:是否只读:
    		true:只读;mybatis认为所有从缓存中获取数据的操作都是只读操作,不会修改数据。
    				 mybatis为了加快获取速度,直接就会将数据在缓存中的引用交给用户。不安全,速度快
    		false:非只读:mybatis觉得获取的数据可能会被修改。
    				mybatis会利用序列化&反序列的技术克隆一份新的数据给你。安全,速度慢
    	size:缓存存放多少元素;
    	type="":指定自定义缓存的全类名;
    			实现Cache接口即可;
    	-->
        
     </mapper>
    
    	3)、我们的POJO需要实现序列化接口
    
    @Alias("emp")
    public class Employee implements Serializable{
          
          
        
    }
    
 * 	以下作为了解:
     * 和缓存有关的设置/属性:
     * 			1)、cacheEnabled=truefalse:关闭缓存(二级缓存关闭)(一级缓存一直可用的)
     * 			2)、每个select标签都有useCache="true"* 					false:不使用缓存(一级缓存依然使用,二级缓存不使用)
     * 			==3)、【每个增删改标签的:flushCache="true":(一级二级都会清除)】==
     * 					增删改执行完成后就会清楚缓存;
     * 					测试:flushCache="true":一级缓存就清空了;二级也会被清除;
     * 					查询标签:flushCache="false"* 						如果flushCache=true;每次查询之后都会清空缓存;缓存是没有被使用的;
     * 			4)、sqlSession.clearCache();只是清楚当前session的一级缓存;
     * 			5)、localCacheScope:本地缓存作用域:(一级缓存SESSION);当前会话的所有数据保存在会话缓存中;
     * 								STATEMENT:可以禁用一级缓存;		

3) Third-party cache integration
a ). Just import the third-party cache package;
b). Import the adaptation package integrated with the third-party cache; official
ones ; c). Use custom cache in mapper.xml

 	<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>

ehcache.xml profile settings

 <!-- 磁盘保存路径 -->
 <diskStore path="D:\44\ehcache" />
 <defaultCache 
   maxElementsInMemory="10000" 
   maxElementsOnDisk="10000000"
   eternal="false" 
   overflowToDisk="true" 
   timeToIdleSeconds="120"
   timeToLiveSeconds="120" 
   diskExpiryThreadIntervalSeconds="120"
   memoryStoreEvictionPolicy="LRU">
 </defaultCache>
</ehcache>
 
<!-- 
属性说明:
l diskStore:指定数据在磁盘中的存储位置。
l defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略
 
以下属性是必须的:
l maxElementsInMemory - 在内存中缓存的element的最大数目 
l maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
l eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
l overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
 
以下属性是可选的:
l timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
l timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
 diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
l diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
l diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
l memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
 -->

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/107043371