Level 1 cache

  • Mybatis enables level 1 caching by default
  • It only caches data in one session, that is, the scope is one session

A session: sqlSession from open to close is a session

Such as:

The second level cache

  • Entity classes (POJOs) need to be serialized

  • Level 2 cache needs to be manually enabled

To enable level 2 caching globally, add a line to your SQL Mapper file:

<cache/>
Copy the code

Add the condition of enabling level 2 cache in myBatis-config

<! -- Notify MyBatis framework to enable level 2 cache -->
<settings>
  <setting name="cacheEnabled" value="true"/>
</settings>
Copy the code
  • The scope is under a namespace
  • The results of all SELECT statements in the mapping statement file will be cached.
  • All INSERT, UPDATE, and DELETE statements in the mapping statement file flush the cache.
  • The cache uses the Least Recently Used algorithm (LRU) algorithm to clear unwanted caches.
  • The cache is not flushed regularly (that is, there are no flush intervals).
  • The cache holds 1024 references to lists or objects (whichever is returned by the query method).
  • The cache is treated as a read/write cache, which means that the retrieved object is not shared and can be safely modified by the caller without interfering with potential changes made by other callers or threads.

The attributes of a cache element can be modified. Such as:

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

This more advanced configuration creates a FIFO cache, refreshed every 60 seconds, that can store up to 512 references to result objects or lists, and the returned objects are considered read-only, so modifying them could cause conflicts among callers in different threads.

  1. The flushInterval property can be set to any positive integer in milliseconds. The default is none,

  2. The size attribute can be set to any positive integer, taking into account the size of the object to be cached and the memory resources available in the runtime environment. The default value is 1024.

  3. The readOnly property can be set to true or false. A read-only cache returns the same instance of the cache object to all callers. Therefore, these objects cannot be modified. This provides a significant performance boost. A read-write cache returns (through serialization) a copy of the cached object. It’s slower, but safer, so the default is false.

Level 2 Cache key points

  • The first time to call SQL under mapper to query user information. The queried information is stored in the level-2 cache area corresponding to the mapper.

  • Call the same SQL in the mapper mapping file of the same namespace for the second time to query user information. It will fetch the result from the corresponding level 2 cache.

  • If the SQL statement in the Mapper mapping file of the same namespace is added, deleted, or modified, and the COMMIT operation is performed. The level-2 cache under the namespace is cleared.

  • Disable Level 2 cache: The default level 2 cache granularity is Mapper level, but if you don’t want to use level 2 cache for a query in the same Mapper file, you need to have more granular control over the cache.

If you set useCache=false in the SELECT tag, you can disable level 2 caching for the current SELECT statement. That is, each query is made to the database. By default, this value is true, indicating that the statement uses level 2 caching.

Disable level 2 caching for the current SELECT statement:

<select id="findBlogForeach" parameterType="map" resultType="blog" useCache="false">
Copy the code

Custom cache