The design principle of MyBatis's level two cache

Source: Internet
Author: User

MyBatis's Level Two cache is a application cache that improves the efficiency of database queries to improve application performance. This article will comprehensively analyze the design principle of MyBatis's level two cache.

1.MyBatis cache mechanism overall design and two-level cache working mode

As shown, when a session is opened, a sqlsession object uses a Executor object to complete the session operation, and the key to MyBatis 's two-level caching mechanism is the Executor object of the fuss. If the user is configured with " cacheenabled=true", then MyBatis creates a Executor object for the sqlsession object, the Executor Object plus an adorner: cachingexecutor, then sqlsession uses the cachingexecutor object to complete the operation request. cachingexecutor for query requests, it first determines whether the query request has a cache result in the level two cache of the application , and returns the cached result directly if there is a query result; To the real Executor object to complete the query operation, then Cachingexecutor will put the query results returned by the real Executor in the cache, and then return to the user.

Cachingexecutor is a Executor decorator, to enhance the function of Executor , so that it has the function of caching query, here used in the design pattern of the decorator mode,

The relationship between Cachingexecutor and Executor interfaces is shown in the following class diagram:

2. MyBatis classification of Level two caches

MyBatis is not simply a cache-cached object for the entire application , it divides the cache into finer, Mapper -level That is, each mapper can have a Cache object, as follows:

A. Assign a cache cache object (using <cache> node configuration) for each mapper;

B. Multiple mapper share a cache cache object (using <cache-ref> node configuration);

A. Assigning a cache cache object to each mapper (using <cache> node configuration)

MyBatis subdivides The level two cache of application levels to the Mapper level, that is, for each mapper.xml, if < is used in it Cache> node, MyBatis creates a cache object for this Mapper , as shown in:

Note: Each of the above cache objects will have a namespace namespace of their own, and will use the Mapper namespace as their ID;

B. Multiple mapper share a cache cache object (using <cache-ref> node configuration)

If you want multiple Mapper to have a single Cache , you can use <cache-ref namespace= "" > node to specify your Mapper which Mapper cache is used.

3. Required conditions for using level two cache

MyBatis support for level two cache granularity is fine, and it specifies whether a query statement uses a level two cache.

Although <cache>is configured in Mapper and the cache object is allocated for this Mapper , this does not mean that we use Mapper The results that are defined in the query statements are placed in the cache object, we must specify whether a selection statement in Mapper supports caching, as shown in the <select> node configuration usecache= "True", Mapper will not support caching features for queries of this select , otherwise, this select query will not be Cache caches. As shown below, the Select statement is configured with usecache= "true", which indicates that the query for this select statement uses a level two cache.

<select id= "selectbyminsalary" resultmap= "Baseresultmap" parametertype= "Java.util.Map" usecache= "true" >

In summary, to enable a Select query to support level two caching, you need to ensure that:

1. MyBatis supports the total switch of level two cache: Global configuration variable parameter cacheenabled=true

2. The mapper of the SELECT statement is configured with a <cache> or <cached-ref> node and is effectively

3. The parameter of the SELECT statement Usecache=true

4. Order of first-level cache and level two cache usage

Note that if your MyBatis uses a level two cache, and your Mapper and Select statements are configured with a level two cache, when you execute a select query, MyBatis will take input from the level two cache, followed by the first level cache, that is, the order in which MyBatis queries the data is:

level two cache ——— > Cache--Database

5. Choice of Level two cache implementation

MyBatis is very flexible in the design of level two cache, it implements a series of cache cache implementation classes, and provides various caching refresh strategies such as Lru,fifo and so on, in addition, MyBatis also allows the user to customize the cache interface implementation, the user needs to implement the Org.apache.ibatis.cache.Cache interface, and then configures the cache implementation class to <cache type= "" > node on the type attribute; In addition, MyBatis supports integration with third-party memory cache libraries such as memecached . In summary, a level two cache with MyBatis has three choices:

1.MyBatis cache implementation provided by itself;

2. User-defined cache interface implementation;

3. Integration with third-party memory cache libraries;

6. Implementation of the level two cache provided by the MyBatis itself

MyBatis itself provides a rich and powerful implementation of the two-level cache, which has a range of cache interface decorators that can satisfy a variety of strategies for caching operations and updates.

MyBatis defines a large number of cache decorators to enhance the cache caching functionality, as shown in the following class diagram.

For each cache , there is a capacity limit, and MyBatis provides various policies to control the cache 's capacity and the cache  Refresh and replace the data in the MyBatis mainly provides the following refresh and displacement strategies:

LRU: (Least recently used), the least recently used algorithm, that is, if the cache is full, the cache records that have been used most recently in the cache are purged, and new records are added;

FIFO: (first in initial out), FIFO algorithm, if the cache capacity is full, then the first entry into the cache of data removed;

Scheduled: Specifies the interval emptying algorithm, which empties the data in the cache at a specified time interval;

6. Write in the back (about the design pattern involved)

In the design of the level two cache, MyBatis used a large number of decorator patterns, such as cachingexecutor, as well as decorators for various cache interfaces. About the decorator mode, readers can read the relevant materials, my other blog post Java design pattern decorator mode for readers reference.

This article is just about the basic principle of mybatis level two cache, about custom level two caching and integration with third-party memory libraries, will be discussed in subsequent articles, please pay attention!

Author's words

This article is one of the series of in-depth understanding of mybatis principles, and if you are interested, please follow other articles in this series ~

Think this article is good, handy point a praise Oh ~ ~ Your encouragement, is I continue to share the powerful power of knowledge!

If you feel that there is a mistake or wrong place, please feel free!

--------------------------------------------------------------------------------------------------------------- --------------------------

This article originates from http://blog.csdn.net/luanlouis/, if need reprint, please indicate source, thank you!

MyBatis column

Http://blog.csdn.net/column/details/mybatis-principle.html?page=1

The design principle of MyBatis's 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.