11 Hours: Disable, clean up the secondary cache, and the cache integrated Ehcache

.1) How to disable the secondary cache

  1 select specific tags to be closed mapper.xml in which fill

<select id="selectStudentById" resultType="student" parameterType="Integer" useCache="false">
        select * from student where stuno=#{stuno}
    </select>

.2) Cleanup: Cleanup same buffer cache

 . 1  SqlSession.close () will be recorded as a cache; (additions and deletions will perform a cache-busting; design reasons to produce dirty data) in the secondary cache, SqlSession.commit () query itself can not commit () .

    SqlSession.commit () will clean up the cache and secondary cache; but it can not clean up the query itself SqlSession.commit secondary cache ()

 2 Add flushCache in select tag = "true" can also clear the cache

.3) hit rate:

  1.zs:0%  ===>50%  ===> 66.6%  ===> 75%

.4) Ehcache integrated secondary cache (there are many third-party cache to Ehcache for example)

  1. The need to import the following three jar package

<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

  2. Write ehcache.xml profile

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
< Ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 
         xsi: noNamespaceSchemaLocation = "HTTP: // Ehcache .org / ehcache.xsd " 
         UpdateCheck =" to false " > 
<-!     when the secondary cache memory exceeds the limit of the object (the number of cache object> maxElementsInMemory) when the file is stored in the hard disk -> 
    < diskStore path =" E : \ U \ Ehcache " /> 

<-! 
    maxElementsInMemory: set the number of in-memory cache object 
    maxElementsOnDisk: set the number of the hard disk cache object 
    overflowToDisk: permanently set the cache does not expire 
    diskPersistent:When the number of objects in-memory cache of more than maxElementsInMemory of metastasis to the hard disk When the number of objects in the cache memory exceeds maxElementsInMemory transferred to the hard disk whether 
    timeToIdleSeconds: when access to more than twice this value, failure of the cache object
    timeToLiveSeconds: a cache object, most storage time (life cycle)
    diskExpiryThreadIntervalSeconds: How often to clean the cache hard disk by a thread in 
    memoryStoreEvictionPolicy: strategy when the maximum cache object processing: LRU value is to delete the least used 
-> 
    < defaultCache
             Eternal = "false" 
            maxElementsOnDisk = "1000000 " 
            maxElementsInMemory =" 1000 " 
            overflowToDisk =" to false " 
            diskPersistent =" to false " 
            timeToIdleSeconds =" 100 " 
            timeToLiveSeconds =" 100 " 
            diskExpiryThreadIntervalSeconds =" 120 " 
            memoryStoreEvictionPolicy =" the LRU " >
    </defaultCache>
</ehcache>

  3. Start the main secondary cache configuration

<! -         opening the secondary cache -> 
        < Setting name = "cacheEnabled" value = "to true" />

  4. Write cache tag mapper

<! -     declare open -> 
< Cache of the type = "org.mybatis.caches.ehcache.EhcacheCache" > 
<! -     If you write the following values will override the global configuration of ehcache.xml -> 
<! - -     <Property name = "maxElementsInMemory" value = "2000" /> -> 
<-!     <Property name = "overflowToDisk" value = "to true" /> -> 
</ Cache >

  5. Test mybatis comes with the same secondary cache

Guess you like

Origin www.cnblogs.com/thisHBZ/p/12458112.html