Mybatis Study Notes-Level 1 cache and level 2 Cache

Source: Internet
Author: User

1. level-1 cache: The hashmap local cache based on perpetualcache. Its Storage scope is session. After the session is flushed or closed, all the caches in the session will be cleared.

2. The second-level cache mechanism is the same as that of the first-level cache, which is also used by default.Perpetualcache HashmapStorage, the difference is that its storage scope is er Er (namespace), and you can customize storage sources, such as ehcache.

3. For the cache data update mechanism, when a scope (level-1 cache session/level-2 cache)Namespace) After the C/u/D operation is performed, the cache in all select statements in this scope will be clear by default.


User. Java entity class

public class User {private int id;private String username;private int age;private String sex;//...}


<select id="getUser" parameterType="int" resultType="com.mybatis.test07.User">select * from users where id = #{id}</select><update id="updateUser" parameterType="com.mybatis.test07.User">update users set username = #{username}, age = #{age} where id = #{id}</update>


Test

Sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (); string Statement = "com. mybatis. test06.usermapper. getuser "; user = session. selectone (statement, 2); system. err. println (User); User = session. selectone (statement, 2); system. err. println (User); Session. close (); // by default, the next level of cache is enabled. When you query the same object twice, only one select operation is performed.
Sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (true); string Statement = "com. mybatis. test07.usermapper. getuser "; user = session. selectone (statement, 2); system. err. println (User); // update the user. setid (11); Statement = "com. mybatis. test07.usermapper. updateuser "; system. err. println (User); Session. update (statement, user); // resend the query statement = "com. mybatis. test07.usermapper. getuser "; user = session. selectone (statement, 2); system. err. println (User); Session. close ();
Sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (true); string Statement = "com. mybatis. test07.usermapper. getuser "; user = session. selectone (statement, 2); system. err. println (User); sqlsession session1 = factory. opensession (true); User = session1.selectone (statement, 2); system. err. println (User); Session. close (); session1.close (); // two different session objects


Level 1 cache cannot be used in three cases: ① session. Sort AchE () is executed; ② cud is executed; ③ not the same session object


The second-level cache uses namespace as the unit, that is, the ing file as the unit. Hibernate second-level cache is in sessionfactory units.

Add <Cache/> to the ing file.

Sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (); string Statement = "com. mybatis. test07.usermapper. getuser "; user = session. selectone (statement, 2); system. err. println (User); // user ([email protected]) [ID = 2, username = Syd, age = 34, sex = male] session. commit (); sqlsession session1 = factory. opensession (); User = session1.selectone (statement, 2); system. err. println (User); // user ([email protected]) [ID = 2, username = Syd, age = 34, sex = male] session1.commit (); Session. close (); session1.close ();


The two objects are not the same, but are not queried from the database for the second time.


1. All select statements in the ing statement file will be cached

2. All insert update Delete statements in the ing statement file refresh the cache.

3. the cache uses the least recently used (least recently used LRU) algorithm to reclaim

4. the cache will be refreshed Based on the specified interval.

5. the cache will store 1024 objects

This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1560167

Mybatis Study Notes-Level 1 cache and level 2 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.