[Mybatis] Mybatis cache

mybatis database provides a caching mechanism to reduce the pressure to improve database performance

mybatis cache is divided into two: a cache, L2 cache

Level cache is SqlSession-level cache, the cached data is only valid for SqlSession

Secondary cache is a mapper level cache, the same namespace public that a cache, so SqlSession is shared

 

Mybatis primary cache and secondary cache execution sequence

1, first determines whether the secondary cache is turned on, if not turned on, and then determines whether a cache is turned on, if not open, Direct Access database

2, if a cache off, even if the data is not open secondary cache, because cache data obtained from the two-level cache

3, generally do not shut cache

4, the secondary cache is not turned on by default

5, if the secondary cache off, directly determines whether there is a data cache, if there is no check on the database

6, if the secondary cache is turned on, the secondary cache is determined to have no data, if there is direct return; if not, the query cache, if there is return, no server searches the database;

 

 

Cache:

  Mybatis level cache is SqlSession level cache, create SqlSession session object at the time of the operation of the database, there is a HashMap to store cached data in the object, this is the current HashMap private session object, other objects can not SqlSession session access.

  

 

  specific process:

      1. The first performance data HashMap select the writing is finished it will be found in the cache the SqlSession

      2. The second will check the implementation of select data from the cache, if you select the same cut-parameters the same, then you can return data from the cache, do not go to the database, thereby enhancing the efficiency

    

  Precautions:

      1. If SqlSession performed DML operations (insert, update, delete), and commit, then mybatis will erase all the current cache data SqlSession cache, so you can ensure the data stored in the cache and the database is always consistent, avoid appear dirty read

      2. When the end of a SqlSession then he inside the cache would not exist, mybatis cache is enabled by default, does not require configuration

      3.mybatis cache is based on the [namespace: sql statement: parameter] to caching, meaning that, when the HashMap to store cached data SqlSession is to use [namespace: sql: parameter] as a key, returned by the query statement as value preservation of. For example: -1242243203: 1146242777: winclpt.bean.userMapper.getUser: 0: 2147483647: select * from user where id = ?: 19

 

Secondary cache:

  A secondary cache mapper level cache, i.e. the same namespace mappe.xml, when a plurality of SqlSession Mapper operation using the same database, the resulting data is cached in the secondary cache area of ​​the same

  Secondary cache is not turned on by default. Open two cache configure global parameter setting

  conf.xml:

<settings> 
        <Setting name = "cacheEnabled" value = "to true" /> Default is false: Close secondary cache 
<settings>

  In userMapper.xml configure:

<Cache eviction = "LRU" flushInterval = "60000" size = "512" readOnly = "true" /> mapper current open at all statements secondary cache

  Here configured with a LRU cache refresh every 60 seconds and a maximum of 512 objects, and the object is read-only but returned

  To disable the current secondary cache select statement, add useCache = "false" by the following:

<select id="getCountByName" parameterType="java.util.Map" resultType="INTEGER" statementType="CALLABLE" useCache="false">

  specific process:

      1. When performing a sqlseesion a select, at the time close the session, results from a query to a secondary cache buffer

      2. When another sqlsession perform select, will first look at his own level cache, if not found, and he went looking for the second level cache, found on return, will not have a database, thus reducing database stress improving performance 

      The above is correct: First check the secondary cache, and then check the cache, and then check the database; even in a sqlSession, the first check will be secondary cache; a query even more so in the namespace;

                 Therefore, the cache execution order is: secondary cache -> cache -> Database

  Precautions:

      1. If SqlSession performed DML operations (insert, update, delete), and commit, then mybatis will erase all the current cache data mapper cache, so you can ensure that the data stored in the cache and the database is always consistent, avoid appear dirty read

      2.mybatis cache is based on the [namespace: sql statement: parameter] to caching, meaning that, when the HashMap to store cached data SqlSession is to use [namespace: sql: parameter] as a key, returned by the query statement as value preservation of. For example: -1242243203: 1146242777: winclpt.bean.userMapper.getUser: 0: 2147483647: select * from user where id = ?: 19

 

Transfer: https://www.cnblogs.com/coloz/p/10613054.html

mybatis cache is divided into two: a cache, L2 cache

Level cache is SqlSession-level cache, the cached data is only valid for SqlSession

Secondary cache is a mapper level cache, the same namespace public that a cache, so SqlSession is shared

 

Mybatis primary cache and secondary cache execution sequence

1, first determines whether the secondary cache is turned on, if not turned on, and then determines whether a cache is turned on, if not open, Direct Access database

2, if a cache off, even if the data is not open secondary cache, because cache data obtained from the two-level cache

3, generally do not shut cache

4, the secondary cache is not turned on by default

5, if the secondary cache off, directly determines whether there is a data cache, if there is no check on the database

6, if the secondary cache is turned on, the secondary cache is determined to have no data, if there is direct return; if not, the query cache, if there is return, no server searches the database;

 

 

Cache:

  Mybatis level cache is SqlSession level cache, create SqlSession session object at the time of the operation of the database, there is a HashMap to store cached data in the object, this is the current HashMap private session object, other objects can not SqlSession session access.

  

 

  specific process:

      1. The first performance data HashMap select the writing is finished it will be found in the cache the SqlSession

      2. The second will check the implementation of select data from the cache, if you select the same cut-parameters the same, then you can return data from the cache, do not go to the database, thereby enhancing the efficiency

    

  Precautions:

      1. If SqlSession performed DML operations (insert, update, delete), and commit, then mybatis will erase all the current cache data SqlSession cache, so you can ensure the data stored in the cache and the database is always consistent, avoid appear dirty read

      2. When the end of a SqlSession then he inside the cache would not exist, mybatis cache is enabled by default, does not require configuration

      3.mybatis cache is based on the [namespace: sql statement: parameter] to caching, meaning that, when the HashMap to store cached data SqlSession is to use [namespace: sql: parameter] as a key, returned by the query statement as value preservation of. For example: -1242243203: 1146242777: winclpt.bean.userMapper.getUser: 0: 2147483647: select * from user where id = ?: 19

 

Secondary cache:

  A secondary cache mapper level cache, i.e. the same namespace mappe.xml, when a plurality of SqlSession Mapper operation using the same database, the resulting data is cached in the secondary cache area of ​​the same

  Secondary cache is not turned on by default. Open two cache configure global parameter setting

  conf.xml:

<settings> 
        <Setting name = "cacheEnabled" value = "to true" /> Default is false: Close secondary cache 
<settings>

  In userMapper.xml configure:

<Cache eviction = "LRU" flushInterval = "60000" size = "512" readOnly = "true" /> mapper current open at all statements secondary cache

  Here configured with a LRU cache refresh every 60 seconds and a maximum of 512 objects, and the object is read-only but returned

  To disable the current secondary cache select statement, add useCache = "false" by the following:

<select id="getCountByName" parameterType="java.util.Map" resultType="INTEGER" statementType="CALLABLE" useCache="false">

  specific process:

      1. When performing a sqlseesion a select, at the time close the session, results from a query to a secondary cache buffer

      2. When another sqlsession perform select, will first look at his own level cache, if not found, and he went looking for the second level cache, found on return, will not have a database, thus reducing database stress improving performance 

      The above is correct: First check the secondary cache, and then check the cache, and then check the database; even in a sqlSession, the first check will be secondary cache; a query even more so in the namespace;

                 Therefore, the cache execution order is: secondary cache -> cache -> Database

  Precautions:

      1. If SqlSession performed DML operations (insert, update, delete), and commit, then mybatis will erase all the current cache data mapper cache, so you can ensure that the data stored in the cache and the database is always consistent, avoid appear dirty read

      2.mybatis cache is based on the [namespace: sql statement: parameter] to caching, meaning that, when the HashMap to store cached data SqlSession is to use [namespace: sql: parameter] as a key, returned by the query statement as value preservation of. For example: -1242243203: 1146242777: winclpt.bean.userMapper.getUser: 0: 2147483647: select * from user where id = ?: 19

 

Transfer: https://www.cnblogs.com/coloz/p/10613054.html

Guess you like

Origin www.cnblogs.com/itplay/p/11183679.html