Cache : enabled by default
- MyBatis cache life cycle and SqlSession consistent.
- An internal cache MyBatis simple design, but without the HashMap defined capacity, lacking in functionality cache.
- MyBatis is the maximum range of an internal cache SqlSession, when a plurality of SqlSession or distributed environment, the database writes can cause dirty data,
- Dirty reason: to perform in a sqlSession several query operation, but in the middle of another sqlSession these query operations to update the database, and this update first sqlSession is no perception, at this time it is still a go inquiry query cache
- A cache miss (did not understand the reasons) using mybatis-spring
Secondary cache: off by default
Life cycle namespace
Step two cache Open: an open global cache (Setting enabled by default)
Two in the corresponding opening in mapper.xml
When the time sqlSession not closed or submit the data to the query does not cache secondary cache
- MyBatis secondary cache is a cache with respect to realized
SqlSession
sharing of data between the cache, while more fine particle size, it is possible tonamespace
level, through the Cache interface class different combinations of controllability is also stronger Cache . - MyBatis in multi-table queries, most likely will be dirty data (cache change a namespace in another namespace is not aware), there are design flaws, conditions for safe use of secondary cache relatively harsh appearance.
- In a distributed environment, due to the default implementation is based Cache MyBatis is bound under local, distributed environment appears to read dirty data, the cache will need to use a centralized interface Cache MyBatis, there are certain development costs, direct use Redis, Memcached distributed caching and other potentially lower cost, security is also higher
to sum up:
- All insert the mapped statement file, update and delete statements will empty the cache (primary and secondary).
- Deeper: https://tech.meituan.com/2018/01/19/mybatis-cache.html