Mybatis: Cache

1. What is the cache [Cache]

  • There is a temporary data in memory.
  • Users often query the data in the cache (memory), users do not need to query data (relational database data files) from the disk query, the query from the cache to improve query efficiency, solve the problem of high-performance concurrent systems .

2. Why use caching

  • Reduce the number of interactions and databases, reduce system overhead and increase system efficiency.

3. What kind of data requires the use of cache

  • Query and often not easy to change data

4, Mybatis cache

  • MyBatis has includes a powerful query caching feature which can very easily customize and configure the cache. Caching can greatly improve query efficiency.
  • MyBatis system defined by default two cache: a cache and secondary cache

    • By default, only the first-level cache is turned on. (SqlSession level cache, also known as a local cache)
    • The need to manually open and secondary cache configuration, he is based namespace level cache.
    • In order to improve the scalability, MyBatis defines the cache interface Cache. We can customize the interface to the secondary cache by implementing Cache

5, L1 cache

Also known as a cache local cache:

  • At the same database queries during the session data will be placed in the local cache.
  • If you later need to obtain the same data, take directly from the cache, you must not go to query the database;

6, a cache miss four cases

  • Different sqlSession
  • sqlSession same, different query
  • Same sqlSession, additions and deletions to the operation carried out between the two inquiries!
  • Same sqlSession, manually clear the cache
@Test
public void testQueryUserById(){
    SqlSession session = MybatisUtils.getSession();
    UserMapper mapper = session.getMapper(UserMapper.class);

    User user = mapper.queryUserById(1);
    System.out.println(user);

    session.clearCache (); // manually clear the cache 

    the User user2 = mapper.queryUserById (. 1 );
    System.out.println(user2);

    System.out.println(user==user2);

    session.close();
}

So, a cache is a map

7, the secondary cache

  • Secondary cache, also known as global cache, cache scope is too low, so the birth of the second-level cache
  • Based namespace level cache, a name space, corresponding to a secondary cache;
  • Working Mechanism

    • A session query a data, this data will be placed in a cache in the current session;
    • If the current session is closed, the corresponding session cache is gone; we want is, the session closed, the data is stored in the cache to the secondary cache;
    • New session query information, you can get content from secondary cache;
    • Different mapper will on their own isolated data corresponding cache (Map); and

Steps for usage:

  • Core configuration file

    <setting name="cacheEnabled" value="true"/>

     

  • Each mapper.xml configured to use secondary cache, this configuration is very simple;
A way:
 < Cache />

Second way:
Examples official =====> View official documentation
< Cache
   eviction = "FIFO" 
  flushInterval = "60000" 
  size = "512" 
  readOnly = "to true" /> 
This more advanced configuration creates a FIFO cache refresh every 60 seconds, can store up to 512 result objects or lists citations, and objects returned are considered read-only, modify them might callers in different threads of conflict.

8. Conclusion

  • Just open the second-level cache, we query the same Mapper, you can get the data in the secondary cache
  • Isolated data will be the default first on a cache
  • Only after the session to submit on or off, the data will be a cache to the secondary cache

Schematic: 

 

 

 

Guess you like

Origin www.cnblogs.com/zitai/p/11831703.html