what is Ehcache. EhCache is a pure Java in-process cache framework, is a widely used open source Java distributed cache, with fast, capable and so on, is the default Cacheprovider hibernate. Ehcache is a distributed cache framework.
Distributed Cache
Our system to improve the system concurrency, performance, general Distributed system Deployment (cluster deployment mode)
Without the use of distributed caches, cached data is stored separately in each service and is inconvenient for system development. Therefore, the cached data is centrally managed using distributed caching. MyBatis cannot implement distributed caching, and it needs to be integrated with other distributed cache frameworks. MyBatis Integrated Ehcache principle
MyBatis provides a cache interface, and if you want to implement your own cache logic, implement the cache interface development.
MyBatis and Ehcache integration, the MyBatis and Ehcache integration packages provide an implementation class for the cache interface.
The MyBatis default implementation of the Cache class is:
Add Ehcache Pack
Download Address
Integrated Ehcache
Configure the type of the cache in the mapper to be the implementation of the Ehcache to the cache interface.
Add the Ehcache configuration file
Configure the Ehcache.xml under Classpath
<ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: Nonamespaceschemalocation= ". /config/ehcache.xsd "> <diskstore path=" F:\develop\ehcache "/> <defaultcache Maxelementsinmemo ry= "maxelementsondisk=" "10000000" eternal= "false" overflowtodisk= "false" Timetoidl eseconds= "timetoliveseconds=" "diskexpirythreadintervalseconds=" Memorystoreevictionp olicy= "LRU" > </defaultCache> </ehcache>
Property Description:
1.diskStore: Specifies where the data is stored on disk.
2.defaultCache: When you create a cache with Cachemanager.add ("Democache"), Ehcache uses the management policy
specified by <defalutCache/> The following properties are required:
# maxelementsinmemory-The maximum number of element cached in memory
# Maxelementsondisk-The maximum number of element that is cached on disk, if 0 is the Infinity
Eternal-Sets whether the cached elements will never expire. If true, the cached data is always valid, and if False then the timetoidleseconds,timetoliveseconds is judged by the
# Overflowtodisk- Sets whether the expired element is cached on disk when the memory cache overflows the
following properties are optional:
# Timetoidleseconds- When the cached data in Ehcache is two times longer than the Timetoidleseconds property value, the data is deleted, the default value is 0, which is infinite idle time
# timetoliveseconds- The valid lifetime of the cached element is 0 by default, which means that element has a lifetime of Infinity
# diskspoolbuffersizemb This parameter setting Diskstore (disk cache) Buffer size. The default is 30MB. Each cache should have its own buffer.
# diskpersistent-whether to enable the disk to save the data in Ehcache when the VM restarts, false by default.
# diskexpirythreadintervalseconds-disk cache cleanup thread run interval, default is 120 seconds. Each 120s, the corresponding thread will do a cleanup of the data in Ehcache
# Memorystoreevictionpolicy-When the memory cache is maximized and a new element is added, the policy of element in the cache is removed. The default is LRU (least recently used), optional lfu (most infrequently used) and FIFO (first-in-one-out)
adjust the cache parameters as required:
<cache type= "Org.mybatis.caches.ehcache.EhcacheCache" >
<property name= "Timetoidleseconds" value= " 3600 "/>
<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>
Level Two application scenarioFor 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.
level Two cache limitations
MyBatis level Two cache for fine-grained data-level cache implementation is not good, such as the following requirements: The product information 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.
Latest Version Ehcache official download address