Seven, Mybatis cache

Mybatis cache:

One, definition:

Cache optimization refers to an effective way to improve the efficiency of program execution through data optimization. For example, I need to query the data of a certain product. After extracting it once, I need to extract it again. Because mysql reads data from the hard disk, the speed of reading data from the hard disk is slow. At the same time, the same records are returned for the first and second extractions, and multiple extractions are unreasonable. We can put the data found in the first query into the memory, and access the value directly from the internal memory when it is retrieved for the second time. The internal access value speed is at least several tens of times faster than that of the hard disk.

Two, Mybatis caching mechanism:

  1. The first level cache is enabled by default, and the cache range is a sqlsession session, which is small

  2. The second-level cache is manually turned on, and the scope belongs to the Mapper Namespace, and the scope is larger

Insert picture description here

Third, the operating rules of the secondary cache

  1. After the second level is turned on, the cache is used by default for all query operations

  2. When the write operation commit is submitted, the namespace cache is forced to be cleared, which is to ensure data consistency. For example, the first user gets a product called infant milk powder, and the second user changes the name. If the cache is not cleared at this time, the first user still gets infant milk powder, so the data obtained and the bottom layer of the database get Is inconsistent.

  3. Configure useCache=false to avoid caching

  4. Configure flushCache=true to force the cache to be cleared

Fourth, turn on the secondary cache

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goods">
    <!--二级缓存配置-->
    <cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>
</mapper>

Five, cache configuration

1. The meaning of secondary cache configuration

There are four main types of removal strategies:

1. eviction: The cache clearing strategy. When the cached object reaches the upper limit, the corresponding algorithm will be automatically triggered to clear the cached object.

  • LRU: The longest unused, remove the object that is not applicable for the longest time (mybatis uses this by default, which can maximize the hit rate of our visit)

  • LFU: Least Recently Used: Remove the object with the lowest recent access rate (used less)

  • FIFO: First in, first out: Remove objects in the order in which they enter the cache, and remove the object that enters first (use less, just understand)

  • SOFT: soft reference: remove objects based on garbage collector status and soft reference rules (JVM-based garbage collector, use less, just understand)

  • WEAK: Weak references: more aggressively remove objects based on garbage collector status and weak reference rules (JVM-based garbage collectors, use less, just understand)

2. flushInterval: The interval for flushing the cache, the unit is milliseconds, it can be set to be longer, using this option can effectively recover the memory in time

Example: flushInterval="60000": Automatically clear the cache every 10 minutes

3. size: the length of the cache, indicating the maximum number of objects that can be cached currently.

  • Whether it is caching a certain entity class or a list collection, it will only be regarded as returning an object. In actual development, it is not recommended to store the list as a cache object in the secondary cache, because the data returned by the list is changeable. Causes a low hit rate (such as selectALL), it is recommended to use selectById that returns a single object, and the hit rate is high

  • The length of size should not be set too small. In the case of sufficient memory, for example, there are 1400 products, the size should be set to 1400, so that all product objects can be stored in this second-level cache, so that queries can be made by id When it can be directly extracted from the memory, the efficiency is very high.

Insert picture description here

4. readOnly : Whether to set to read-only

  • Set to true, it means to return to the read-only cache, each time the cache object itself is retrieved from the cache, this kind of execution efficiency is higher

  • If set to false, it means that each time a copy of the cached object is retrieved, and the object retrieved each time is different, which is more secure

2. Cache settings for other tags

  1. ​UseCache: Whether to use the cache, you can set true or false. It is not recommended to use the selectALL query global configuration, because the amount of data is too large, which will cause a lot of pressure on the memory, and because the data returned by the list is changeable. As a result, the hit rate is low, just set it to false.
  2. flushCache: Whether to force the cache to be cleared after sql is executed
  • Can be set to true or false, if set to true, the effect is exactly the same as commit

  • Can be used for write and query operations.

  • After setting this, it will not be put into the cache as well

<insert id="insert" parameterType="com.imooc.mybatis.entity.Goods" useGeneratedKeys="true" flushCache="true">

This means that the cache will be cleared immediately after inserting the data successfully

Sixth, the cache hit rate:

For example, if it is executed 2 times, the first time is to query from the database without
using the cache, and the second time is to retrieve from the cache. At this time, the hit rate is 0.5. If the query is performed 3 times, the hit rate is 0.66. The higher the hit rate, the higher the cache usage rate, and the better the program optimization effect.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/112410596