Home >
Java >
javaTutorial >
What is query cache? Introduction to the usage of MyBatis query cache
What is query cache? Introduction to the usage of MyBatis query cache
零下一度
Release: 2017-06-25 10:56:07
Original
2402 people have browsed it
Please indicate the source for reprinting:
As mentioned earlier: Spring+SpringMVC+MyBatis in-depth learning and construction (7)-MyBatis delayed loading
1. What is query cache
Mybatis provides query caching to reduce database pressure and improve database performance.
Mybatis provides first-level cache and second-level cache.
The first-level cache is a SqlSession-level cache. When operating the database, you need to construct a sqlSession object. There is a data structure (HashMap) in the object for storing cache data. The cache data areas (HashMap) between different sqlSession do not affect each other.
The second-level cache is a mapper-level cache. Multiple sqlSession operates the same Mapper's sql statement. Multiple sqlSession can share the second-level cache. The second-level cache is across sqlSession.
Why use caching?
If there is data in the cache, there is no need to obtain it from the database, which greatly improves system performance.
2. Level 1 Cache
2.1 Working Principle of Level 1 Cache
## The first time the query is initiated for user information with user ID 1 , first find out whether there is user information with id 1 in the cache. If not, query the user information from the database. Get the user information and store the user information in the first-level cache. If sqlSession performs a commit operation (performing insert, update, delete), clear the first-level cache in sqlSession. The purpose of this is to store the latest information in the cache and avoid dirty reads. The second time it is sent to query the user information with user ID 1, first find out whether there is user information with ID 1 in the cache. If there is in the cache, get the user information directly from the cache. 2.2 First-level cache test Mybatis supports first-level cache by default and does not need to be configured in the configuration file. Follow the first-level cache principle steps above to test.
2.3 Level 1 Cache Application The official development is to integrate mybatis and spring, and the transaction is controlled in the service. A service method includes many Mapper method calls. service{ //When execution starts, start the transaction and create the SqlSession object //The first time the mapper method is called findUserById(1) //Call the mapper method findUserById(1) for the second time to get data from the first-level cache //The method ends and sqlSession is closed}If two service calls are executed to query the same user information, the first-level cache will not be used. Because the session method ends, sqlSession will be closed and the first-level cache will be cleared. 3. Second-level cache3.1 Second-level cache principle
First enable the second-level cache of mybatis. sqlSession1 queries the user information with user ID 1. After querying the user information, it regrets that the query data is stored in the secondary cache. If sqlSession3 executes SQL under the same mapper and executes commit submission, the data in the second-level cache area under the mapper will be cleared. sqlSession2 queries the user information with user ID 1, checks whether data exists in the cache, and if so, directly retrieves the data from the cache. The difference between the second-level cache and the first-level cache: the scope of the second-level cache is larger, and multiple sqlSession can share a UserMapper's second-level cache area. UserMapper has a second-level cache area (divided by namespace), and other mappers also have their own second-level cache areas (divided by namespace). Each namespace mapper has a second-level cache area. If the namespaces of two mappers are the same, the data obtained by the two mappers executing SQL queries will be stored in the same second-level cache area. 3.2 Turn on the second-level cache Mybatis’s second-level cache is mapper scope level. In addition to setting the main switch of the second-level cache in SqlMapConfig.xml, it must also be turned on in the specific mapper.xml. Level 2 cache. Add to the core configuration file SqlMapConfig.xml:
<setting name="cacheEnabled" value="true"/>
Copy after login
Description
Allowed values
Default value
##cacheEnabled
Configure global on/off settings for all caches under this configuration file.
diskStore: Specify the storage location of data on the disk.
defaultCache: When creating a Cache with CacheManager.add ("demoCache"), EhCache will adopt the management strategy specified by .
The following properties are required:
maxElementsInMemory: The maximum number of elements cached in memory.
maxElementsOnDisk: The maximum number of elements cached on disk, if 0 means infinity.
eternal: Set whether cached elements will never expire. If it is true, the cached data is always valid. If it is false, you have to judge it based on timeToIdleSeconds and timeToLiveSeconds.
overflowToDisk: Set whether to cache expired elements to disk when the memory cache overflows.
The following attributes are optional:
timeToIdleSeconds: When the time between two accesses to the data cached in EhCache exceeds the value of the timeToIdleSeconds attribute, the data will be deleted. Default value It is 0, which means the idle time is infinite.
timeToLiveSeconds: The effective lifetime of the cache element. The default is 0, which means the element survival time is infinite.
diskSpoolBufferSizeMB: This parameter sets the buffer size of DiskStore (disk cache). The default is 30MB, each Cache should have its own buffer.
diskPersistent: Whether to enable the disk to save data in EhCache when the VM is restarted. The default is false.
diskExpiryThreadIntervalSeconds - The disk cache cleanup thread running interval, the default is 120 seconds. Every 120s, the corresponding thread will clean the data in EhCache.
memoryStoreEvictionPolicy - When the memory cache reaches the maximum size and a new element is added, the policy of removing elements from the cache is used. The default is LRU (least recently used), the options are LFU (least frequently used) and FIFO (first in, first out).
4.6 Test program
Same as 3.4
4.7 Second-level cache application scenario
For query requests with many accesses and users do not have real-time requirements for query results High, at this time, mybatis second-level caching technology can be used to reduce database access and improve access speed. Business scenarios include: time-consuming statistical analysis SQL, telephone bill query SQL, etc.
The implementation method is as follows: by setting the refresh interval, mybatis automatically clears the cache at intervals, and sets the cache refresh interval flushInterval according to the frequency of data changes, such as setting it to 30 minutes, 60 minutes, 24 hours, etc., according to Depends on demand.
4.8 Second-level cache limitations
Mybatis second-level cache is not good at fine-grained data level caching, such as the following requirements: caching product information, due to the number of product information query visits Large, but users are required to query the latest product information every time. At this time, if you use mybatis's second-level cache, it will not be possible to refresh only the cached information of the product when a product changes and not refresh the information of other products, because mybatis's The second-level cache area is divided into mappers. When a product information changes, all cached data of all product information will be cleared. To solve such problems, the business layer needs to cache data in a targeted manner based on needs.
If this article is helpful to you, please tip me on WeChat~
The above is the detailed content of What is query cache? Introduction to the usage of MyBatis query cache. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn