2021-10-06 cache in mybatis

Keywords: Mybatis Cache

8.1 L1 cache

MyBatis has built-in a powerful transactional query caching mechanism, which can be easily configured and customized.

By default, only the local session cache, i.e. L1 cache, is enabled. It only caches the data in a session, i.e. an sqlsession.

Cache invalidation:

  • Query different things

  • Adding, deleting and modifying may change the original data, so the cache will be refreshed

  • Query different Mapper.xml

  • Manually refresh cache

 sqlSession.clearCache();//Manual cache cleanup

The L1 cache is enabled by default and is only valid in one sqlsession. It is essentially a map.

8.2 L2 cache

L2 cache is also called global cache. Its scope is a namespace, and a namespace corresponds to a L2 cache

Working mechanism

  • When a session queries a data, the data will be placed in the first level cache

  • If the current session is closed, the L1 cache will disappear. What we want is that after the L1 cache expires, it will be saved in the L2 cache

  • When a new session queries the data, it reads the data from the L2 cache

  • The data found by different mapper s will be placed in their own cache (map)

The global cache is explicitly enabled. Although it is enabled by default, it is obvious to declare it

   <!--Show global cache enabled-->
     <setting name="cacheEnabled" value="true"/>

To enable the global L2 cache, you only need to add a line to your SQL mapping file:

<cache/>

However, writing only the above will result in an error, because the L2 cache must be set to read-only. If it is not set, the entity class must be instantiated.

<cache
  eviction="FIFO"
  flushInterval="60000"
  size="512"
  readOnly="true"/>

This more advanced configuration creates a FIFO cache, which is refreshed every 60 seconds. It can store up to 512 references of the result object or list, and the returned objects are considered read-only. Therefore, modifying them may conflict with callers in different threads.

Available purge strategies are:

  • LRU – least recently used: removes objects that have not been used for the longest time.

  • FIFO – first in first out: remove objects in the order they enter the cache.

  • SOFT – SOFT reference: removes objects based on garbage collector status and SOFT reference rules.

  • WEAK – WEAK references: remove objects more actively based on garbage collector status and WEAK reference rules.

The default purge policy is LRU.

Flush interval

size (number of references)

readOnly

Summary:

  • As long as the L2 cache is enabled, it is valid under the same mapper

  • All data will be put in the first level cache first

  • Only when the session is committed or closed will it be committed to the L2 cache

8.3 principle of cache

 

8.4 use of custom cache Ehcache

1. Guide coordinates

<!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.2.1</version>
</dependency>

2. Enable L2 cache in mapper

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

3. Import the ehcache.xml configuration file

<?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="false">
​
    <diskStore path="./tmpdir/Tmp_EhCache"/>
​
    <defaultCache
        eternal="false"
        maxElementsInMemory="10000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="1800"
        timeToLiveSeconds="259200"
        memoryStoreEvictionPolicy="LRU"/>
​
    <cache
        name="cloud_user"
        eternal="false"
        maxElementsInMemory="5000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="1800"
        timeToLiveSeconds="1800"
        memoryStoreEvictionPolicy="LRU"/>
</ehcache>

Posted by slick101 on Wed, 06 Oct 2021 09:15:00 -0700