1. Why you need to use caching:
A ) if the same query is made in a very short time, then their results are likely to be the same, and the database is a very consuming resource. If there's a layer of caching, it'll greatly reduce the consumption of resources.
2. Mybatis cache.
A cache is SqlSession, which is implemented internally by a hashmap, whose key is the primary key of the database record. Each SqlSession is independent of each other.
3. The lifecycle of the mybatis cache:
A. SqlSession creates a new object when a database session is opened, with a new executor object in the SqlSession object, which holds a new perpetualcache object. As the session ends, the SqlSession object and its internal executor object and perpetualcache objects are released.
B. If SqlSession calls the close ( ) method, the cache perpetualcache object is released, and the cache isn't available.
C. If SqlSession calls clearcache ( ), the data in the perpetualcache object is empty, but the object is still available.
Any update operation in SqlSession. ( update ( ), delete ( ), insert ( ) ), 都 会 清空 PerpetualCache 对象 的 数据, 但是 该 对象 可以 继续 使用;.
4. Mybatis cached query flow.
For a query, for a query, build a key value based on, params, RowBounds, and retrieve the cache result for the corresponding key value in the cache cache.
2. Determine whether the data data from the cache is null, or if it's hit.
3. If hit, return the cache result directly.
4. If you don't want to:
4. 1 to query data in database, get query results.
4. 2 the result of the key and the query is stored as key, value pair to the cache.
4. 3. Returns the result of the query.
The order in which the cache and the cache are used:
Note that if your mybatis uses a cache, and your mapper and select statements are also configured using a cache, then the mybatis is from the cache cache, followed by a cache, that's, the order in which the mybatis query data is:
Cache cache ---> level cache> database
If there's no, cache, the query level is cached, and if there's a cache in one cache, the results of the query are stored in the cache cache and returned to the user, if no database is logged, and the return to the cache is returned to the user.
The configuration of the cache:
To enable a select query to support a cache, you need to be guaranteed:
1. MyBatis支持二级缓存的总开关：全局配置变量参数 cacheEnabled=true <!-- 二级缓存开启 --><settingname="cacheEnabled"value="true"/>2.该select语句所在的Mapper，配置了<cache> 或<cached-ref>节点，并且有效 <!-- 开启当前mapper的namespace下的二级缓存， 配置了一个LRU缓存，并每隔60秒刷新，最大存储512个对象，而且返回的对象被认为是只读的 --><cacheeviction="LRU"flushInterval="60000"size="512"readOnly="true"></cache>3. 该select语句的参数 useCache=true 如下： <selectid="selectByMinSalary"resultMap="BaseResultMap"parameterType="java.util.Map"useCache="true">
The cache cache is a processing method that's full of cache capacity:
For each cache, there's a capacity limit, and each of them provides a variety of strategies to control cache cache capacity, and to refresh and replace the data. Mybatis provides several of the following refresh and replacement strategies:
Lru: ( least recently used ), the most recently used algorithm, that's, if the capacity is full in the cache, the cache records that are recently used
Fifo: ( first in first out ), the first algorithm, if the capacity in the cache is full, then the first entry in the cache is removed;
Scheduled schedule: specifies the time interval to empty the algorithm, which clears the data in the cache cache at a specified interval of time;
The mybatis isn't simply a cache cache object for the entire application, which divides the cache into a finer, that's, the mapper level, that's, every mapper can have a cache object, as follows:
A. Allocate a cache cache object for each mapper ( using node configuration ).
More than one mapper shared a cache cache object ( using node configuration );
A. Assign a cache cache object for each mapper ( use node configuration ).
The mybatis splits the cache level into the mapper level, which is for each mapper. Xml, and the node creates a cache cache object for this mapper, as in the following illustration:
: each of the above cache objects will have a namespace 命名空间, 并且 会 将 Mapper 的 namespace 作为 它们 的 ID; that itself belongs to
More than one mapper share a cache cache object ( using node configuration )
If you want more than one mapper to public a cache, you can use nodes to specify which mapper 's cache cache to use for your mapper.