MySQL cache: First-level cache and level two cache

Source: Internet
Author: User

First-level caching:

Also known as local cache, sqlsession level cache. The first-level cache is always on, and the data queried during the same session with the database is placed in the local cache.

If you need to get the same data, take it directly from the cache and no longer check the database.

Four scenarios in which the first-level cache fails:

1.sqlSession different.

eg


sqlsession session = Sqlsessionfactory.opensession ();

try {Employee map = Mapper.getemployeebyid (1);
Employee map2 = Mapper.getemployeebyid (1);
         SYSTEM.OUT.PRINTLN (map = = MAP2);




Output is true database is queried only once, MAP2 cache results

  

@Testpublic void test01 () throws IOException {sqlsessionfactory sqlsessionfactory = Getsqlsessionfactory (); sqlsession session = Sqlsessionfactory.opensession ();                 Sqlsession Session2 = Sqlsessionfactory.opensession (); Employeemapper mapper = Session.getmapper (employeemapper.class);                 Employeemapper mapper2 = Session.getmapper (Employeemapper.class); try {Employee map  = Mapper.getemployeebyid (1);                         Employee map2  = Mapper.getemployeebyid (1); SYSTEM.OUT.PRINTLN (map = = MAP2); Session.commit ();        } finally {            session.close ();        }} The output is false two different sqlsession

2.sqlSession is the same, the query conditions are different. Because of different cache conditions, there is no data in the cache.

3.sqlSession same, in two times the same query conditions in the middle of the deletion and modification operations. (Because the intermediate additions and deletions may change the data in the cache, so it cannot be used)

4.sqlSession is the same, manually emptying the first-level cache.

eg

@Testpublic void test01 () throws IOException {sqlsessionfactory sqlsessionfactory = Getsqlsessionfactory (); sqlsession session = Sqlsessionfactory.opensession (); Employeemapper mapper = Session.getmapper (Employeemapper.class); try {Employee map  = Mapper.getemployeebyid (1);                         Session.clearcache ();                         Employee map2  = Mapper.getemployeebyid (1); SYSTEM.OUT.PRINTLN (map = = MAP2); Session.commit ();        } finally {            session.close ();        }} The output is false. Cache invalidation due to manual clearing of cache

Second-level cache: Global Cache; namespace level-based caching. A namespace corresponds to a level two cache.

Working mechanism: 1. A session, querying a single piece of data, which is placed in the first-level cache of the current session.

2, If the session is closed , the data in the primary cache is saved with a level two cache. The new session query information will refer to level two cache.

3.sqlSession ====> Employee====>employee

Sqlsession ====>departmentmapper=====>department

Data isolated from different namespace will be placed in their corresponding cache.

Effect: The detected data is first placed in a primary cache, and only one cache is closed or committed, and the primary cache data is transferred to level two cache

Steps to use:

1. Turn on the global cache configuration. <settings> <setting name="cacheenabled" value="true"/> </settings>

2. Because it is namespace level, you need to configure level two cache with each xxxmapper.xml <cache></cache>

<cache flushinterval= "60000" size= "Up" readonly= "true" eviction= "FIFO" type= "/>

Eviction: Cache Recycle policy:

                     LRU – Least Recently used: Removes objects that are not used for the longest time.

                     FIFO – First in and out: removes them by the order in which they are entered into the cache.

                     SOFT – Soft Reference: Removes objects based on the garbage collector state and soft reference rules.

                     WEAK – Weak reference: More aggressively remove objects based on the garbage collector state and weak reference rules.

Flushinterval: Cache refresh interval. How often the cache is emptied and not emptied by default. Sets a millisecond value.

ReadOnly: Whether it is read-only. True:mybatis that all operations that fetch data from the cache are read-only and do not modify the data.

MyBatis in order to speed up the acquisition, the data in the cache will be referred to the user directly. Not safe, fast.

False:mybatis feel that the data obtained may be modified. MyBatis uses serialization and deserialization techniques to clone a new piece of data to the user. Safe and fast.

Size: How many elements are placed in the cache.

Type: Specifies the full class name of the custom cache. Implement the Cache interface.

The 3.pojo needs to implement a sequence-swapping interface.

and cache-related Configuration/properties:

1.cacheenabled: If False, turn off level two cache without shutting down the first level cache.

2. Each select tag has usercache= "true" property: There is no effect on the first-level cache. Set to False, the level two cache is invalidated.

3. Each additions and deletions are flushcache= "true" property: Both the first level cache and the level two cache are emptied.

4. flushcache= the "false" attribute in the query label: If set to true, the first level two cache will be emptied and the cache will not be used.

5.sqlsession.clearn (): related to the session, only the first level of cache is cleared.

6.localCacheScope: <SETTINGS> <setting  name= " < Span class= "PLN" > localcachescope " value= "SESSION" /> </settings> local cache scope.

< Span class= "ATV" > level cache < span class= "tag" > < Span class= "pun" > Session: All data for the current session is saved to the reply cache. STATEMENT: Disables first-level caching.

 

< Span class= "ATV" > < span class= "tag" > cache first came in to check for level two cache, level two cache did not go to the first level cache , the first-level cache did not go to the database. Secondary cache-----> Level cache--------> database.

< Span class= "pun" > < Span class= "PLN" >< span class= "tag" > custom cache  implements Cache, rewrite the Save methods in the interface, such as Save to Redis.

< Span class= "ATV" > < span class= "tag" > custom cache references MyBatis official website---> Project git code base----->https://github.com/mybatis----> Reference various consolidation caches

MySQL cache: First-level cache and level two cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.