1 . 1mybatis and Ehcache Cache framework consolidation generally does not use MyBatis to manage caches, but uses other caching frameworks to manage caches, because other cache frameworks manage caching more efficiently because others specialize in caching and mybatis specialized in SQL statements, The MyBatis level two cache maintains cached data through Ehcache. 1.1. 1 Distribution Cache distributes the cached data data for distributed management. The user initiates the request, first will select the different server according to the load, if the user has logged on in server 1 and server 2, then puts the user's session respectively in server 1 and server 2 is not possible, therefore puts the user's information in the remote server cluster unified management. If you use MyBatis cache, then the MyBatis cache will be in server 1 and server 2 respectively, the cache can not achieve unified management.
1.1 The 1mybatis and Ehcache ideas can be managed by the MyBatis and Ehcache framework to host the ehcache of the cached data. A cache interface is provided in the MyBatis, so that cached data can be managed flexibly as long as the caching interface is implemented. (source code below)
Default implementation in MyBatis: (source code below)
1.1. 1 Downloads and Ehcache integrated jar packages
Ehcache to expand the MyBatis cache management is to implement the cache interface: (source code below)
1.1. 1 Configuration Ehcache.xml
1.1. 1 Integration Test (Consolidation: Import jar package, write ehcache.xml) add Ehcache configuration in Mapper.xml:<!--turn on level two cache-<!--units: milliseconds--<cache type="Org.mybatis.caches.ehcache.EhcacheCache"> <!--Two-level cache type is the implementation class of Ehcache, the default is MyBatis cache, and <property name="Timetoidleseconds"Value="12000"/> <property name="Timetoliveseconds"Value="3600"/> <!--with ehcache parameters maxelementsinmemory-<property name="Maxentrieslocalheap"Value=" +"/> <!--with ehcache parameters Maxelementsondisk-<property name="Maxentrieslocaldisk"Value="10000000"/> <property name="Memorystoreevictionpolicy"Value="LRU"/> </cache>1The scenario for a level 22 cache is a high-frequency query and a low-frequency variable suggests using a level two cache. For access to many query requests and users of query results real-time requirements are not high, at this time can use MyBatis two cache technology to reduce database access, improve access speed, business scenarios such as: time-consuming statistical analysis of SQL, telephone Bill query SQL. The implementation is as follows: By setting the refresh interval, the cache is automatically emptied by mybatis at intervals, and the cache refresh interval is set according to the data change frequency flushinterval, for example, set to 30 minutes, 60 minutes, 24 hours, etc., depending on the needs. 1. 3mybatis limitations MyBatis Level Two cache for fine-grained data-level cache implementation is not good, such as the following requirements: The product information to cache, because the product information query access is large, but require users every time can query the latest product information, At this point, if you use MyBatis's level two cache, you cannot realize that when a commodity changes, only the cache information of the product is refreshed and the information of other goods is not refreshed, because Mybaits's level two cache area is divided by mapper, and when an item information changes, the cached data of all commodity information is emptied. Addressing such issues requires a targeted cache of data at the business level based on demand.
mybatis0210 MyBatis and Ehcache cache framework Integration