mybatis-- a cache, L2 cache

A, Mybatis cache

● MyBatis has includes a powerful query caching feature which can very easily customize and configure the cache. Ribbon save the query efficiency can be greatly improved.

● MyBatis system defined by default two cache: a cache and secondary cache

  ○ By default, only a cache turned on. (SqlSession level cache, also known as a local cache)

  ○ secondary cache need to manually open and configuration, he is based namespace level cache.

  ○ In order to improve scalability, MyBatis defines the cache interface Cache. We can customize the interface to the secondary cache by implementing Cache

 

Summary: The role of the cache is to improve the efficiency of queries.

 

Second, the cache

● level cache is also called a local cache

  ○ with the same query to the database during the session data will be placed in the local cache.

  After ○ If you need to get the same data, take directly from the cache, no need to go to query the database.

 

Summary: The cache is enabled by default, only one Sqlsession effective, that is, to get connected to close the connection this range segment!

 

Third, the secondary cache

● secondary cache, also known as global cache, cache scope is too low, so the birth of L2 cache.

● based namespace level cache, a namespace (namespace) corresponding to a two-level memory

● working mechanism

  ○ a conversation a query data, this data will be placed in a cache in the current session;

  ○ If the current session is closed, this will be saved in the secondary cache;

  ○ new session query information, you can get content from secondary cache;

  Different ○ mappe isolated data will be placed in their own corresponding cache (map) in;

 

Open secondary cache step of:

1. Add a settings tab in the core profile, as follows:

    <settings>
        <setting name="cacheEnable" value="true"/>
    </settings>

 

 2. Add a line in the SQL mapping file:

<cache/>

 

You can also customize parameters

<cache
  eviction="FIFO"
  flushInterval="60000"
  size="512"
  readOnly="true"/>

This more advanced configuration creates a FIFO buffer, 60 seconds to refresh, store up to 512 references to result objects or lists, and objects returned are considered read-only at intervals, and therefore they may be modified in different threads the caller conflict.

Clear strategies available:

  • The LRU  - Least Recently Used: Removes objects that have not been used for a long time.
  • The FIFO  - First In First Out: order by object into the cache to remove them.
  • SOFT  - Soft Reference: Removes the object referenced rule-based garbage collector state and soft.
  • WEAK  - Weak Reference: More aggressively based on the garbage collector state and rules of Weak References remove the object.

The default purge policy is LRU.

flushInterval (refresh interval) attribute can be set to any positive integer value is set should be a reasonable amount of time in milliseconds units. The default is not set, that is not the refresh interval, the cache is only refreshed when the call statement.

size (reference number) attribute can be set to any positive integer, to be noted that the memory resources to be cached and the size of the object operating environment available. The default value is 1024.

readOnly (read-only) attribute can be set to true or false. Read-only cache will return the same to all callers cached object instance. Therefore, these objects can not be modified. This provides a significant performance boost. And read-write cache will return a copy of the cached objects (serialization). Some slower speed, but more security, so the default value is false.

 

note:

  1. All data will first be placed in a cache, only if the session is submitted or closed, will be submitted to the secondary cache!

   2. If the secondary cache is turned on, mybatis will start to extract data in the secondary cache. If the data is not found in the secondary cache, the cache search again. One can not find the cache data, you will go to query the database and save the query results to a cache.

 

Guess you like

Origin www.cnblogs.com/bear7/p/12515105.html