Mybatis cache (first level cache, second level cache)
Mybatis cache is divided into first level cache and second level cache. Mybatis first level cache is enabled by default; the second level cache global switch is also enabled by default, but CacheNamespace needs to be configured to take effect!
Take Mybatis-plus as an example
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
default-statement-timeout: 300
# mybatis二级缓存全局开关
cache-enabled: true
# mybatis一级缓存作用域(session、statement)
local-cache-scope: session
One, Mybatis first level cache
1. The cache mode is read-only, returning the object itself instead of a copy;
2. It cannot be disabled, but the scope of scope can be adjusted through local-cache-scope: statement; the scope of statement is "equivalent" to disabled (a statement can contain multiple SQL, and it cannot be completely disabled at this time).
Two, Mybatis secondary cache
1. The cache mode is read-write mode by default, and returns a copy of the object (object serialization), which can be adjusted by setting the readWrite property.
@CacheNamespace(flushInterval = 600000, size = 4096, readWrite = true)
2. Can be disabled
mybatis-plus:
configuration:
# mybatis二级缓存全局开关
cache-enabled: false
Cache default implementation class: org.apache.ibatis.cache.impl.PerpetualCache
- PerpetualCache uses local memory (use with caution in cluster mode);
- Use the decorator design pattern to provide richer functions for the cache class org.apache.ibatis.mapping.CacheBuilder#setStandardDecorators.
3. Automatically refresh the cache
When data is modified or deleted under the same namespace, the cache is automatically refreshed. To refresh the cache under other namespaces, use the @CacheNamespaceRef annotation.
4. Manually refresh the cache
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.CommParamDefEntity;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
/**
* @author ouruyi
*/
public interface CommonMapper extends BaseMapper<CommParamDefEntity> {
/**
* 刷新Mybatis二级缓存(一级缓存也会刷新)
* @return 'x'
*/
@Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
@Select("SELECT 'x' AS X FROM DUAL")
String flushCache();
}
Three, performance tuning
- Avoid dynamic parameters as query parameters; for example, the current system time as a query parameter causes the mybatis cache to become invalid;
- redis null value breakdown
- Separate hot and cold data. It is not recommended to put cold data in the cache to prevent the cache from being full. (LRU)
- Appropriate log implementation class
mybatis-plus:
configuration:
# 此处为mybatis-plus通过system.out输出日志到控制台,影响性能(禁用可提升数倍)
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
To:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
- Connection pool configuration
druid:
filters: stat,config
min-idle: 10
max-active: 100
max-pool-prepared-statement-per-connection-size: 20
min-evictable-idle-time-millis: 300000
initial-size: 10
max-wait: 60000
time-between-eviction-runs-millis: 120000
pool-prepared-statements: true
# 高并发性能低
test-on-borrow: false
test-on-return: false
test-while-idle: true
# 持活,检测间隔默认1分钟(不宜超过3分钟,华为防火墙跨网段切断空闲连接时限3分钟)
keep-alive: false
validation-query: SELECT 'x' FROM DUAL
# 校验超时1秒
validation-query-timeout: 1
- Suitable thread pool, database connection pool