mybatis the secondary cache integrated distributed cache implemented ehcache

 mybatis comes secondary cache, but this cache is the work of a single server, distributed cache can not be achieved. So what is it distributed cache? Suppose there and 2, when two users to access a server accesses the server 1, after the query cache will be placed on a server, assuming that users now have access to the server is 2, then he would not be on the server 2 just get the cache, as shown below:

    To solve this problem, we have to find a distributed cache, designed to store the cached data, so that different servers to cache data stored to it there, the data-fetch buffer is also taken from it, as shown below: 

 

    As shown above, between several different servers, we use third-party caching framework, caches are placed in this third-party framework, then no matter how many servers, we can get the data from the cache.

 Here we introduce integration with third-party frameworks ehcache of mybatis.

 Beginning mentioned above, mybatis provides a cache interface if you want to implement your own caching logic, interface development to achieve cache. mybatis itself implements a default, but this can not be achieved to realize the cache distributed cache, so we have to do it yourself. ehcache distributed cache can, mybatis provides a ehcache implementation class for cache interface, this class mybatis and ehcache integrated package in.

①, introduced mybatis-ehcache integration package (uppermost contains source code)

<dependency>
	<groupId>org.mybatis.caches</groupId>
	<artifactId>mybatis-ehcache</artifactId>
	<version>1.1.0</version>
</dependency>

 

②, open the cache in the global configuration file mybatis-configuration.xml

<!--开启二级缓存  -->
<settings>
    <setting name="cacheEnabled" value="true"/>
</settings>

③, integration ehcache cache map file xxxMapper.xml file

<!-- 开启本mapper的namespace下的二级缓存
    type:指定cache接口的实现类的类型,
不写type属性,mybatis默认使用PerpetualCache作为默认的二级缓存,不具有分布式缓存的能力
要和ehcache整合,需要配置type为ehcache实现cache接口的类型
-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>

④, configure the cache parameters

Ehcache.xml create a new file in the classpath directory, and add the following configuration:

<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="F:\develop\ehcache"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> </ehcache>

diskStore:指定数据在磁盘中的存储位置。
 defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略
以下属性是必须的:
 maxElementsInMemory - 在内存中缓存的element的最大数目 
 maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
 eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
 overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
以下属性是可选的:
 timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
 timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
 diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
 diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
 diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
 memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)

Guess you like

Origin www.cnblogs.com/ww25/p/11075558.html