Ehcache configuration: Use Spring + SpringMVC + Mybatis or have shiro, ehcachespringmvc
The establishment of the SSM framework is not described.
This article mainly explains how to add ehcache to SSM.
1: first, the jar required by pom. xml
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.0.1</version> </dependency>
2: add the ehcache. xml configuration file to src/main/resources. The path varies with your framework (everyone knows)
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir" /> <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> <cache name="testCache" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /></ehcache>
<! -- Here is the description
Name: the unique identifier of the Cache.
MaxElementsInMemory: Maximum number of cached objects in memory
MaxElementsOnDisk: Maximum number of cached objects in the disk. If it is 0, it indicates infinity.
Eternal: whether the Element is permanently valid. Once set, timeout does not work.
OverflowToDisk: This attribute is configured. When the number of elements in the memory reaches maxElementsInMemory, Ehcache writes the elements to the disk.
TimeToIdleSeconds: sets the allowed idle time before the Element expires. This attribute is optional only when the element is not permanently valid. The default value is 0, that is, the idle time is infinite.
TimeToLiveSeconds: sets the allowed survival time of an Element before it expires. The maximum time is between the creation time and the expiration time. Used only when the element is not permanently valid. The default value is 0. That is, the element survival time is infinite.
DiskPersistent: Indicates whether to cache data during VM restart.
DiskExpiryThreadIntervalSeconds: disk failure thread running interval. The default value is 120 seconds.
DiskSpoolBufferSizeMB: this parameter sets the cache size of DiskStore (disk cache. The default value is 30 MB. Each Cache should have its own buffer.
MemoryStoreEvictionPolicy: When the maxElementsInMemory limit is reached, Ehcache clears the memory according to the specified policy. The default policy is LRU (least recently used ). You can set it to FIFO (first-in-first-out) or LFU (rarely used) -->
3: In the spring-mybatis.xml, if the encache Configuration
<! -- Use ehcache cache --> <bean id = "ehCacheManager" class = "org. springframework. cache. ehcache. ehCacheManagerFactoryBean "> <property name =" configLocation "value =" classpath: ehcache. xml "/> </bean
-------- Focus ---------
If shiro is integrated in your framework, you need to change it here. The specific configuration is as follows:
<! -- MyBatis uses ehcache to cache start --> <bean id = "ehCacheManager" class = "org. springframework. cache. ehcache. ehCacheManagerFactoryBean "> <property name =" configLocation "value =" classpath: ehcache. xml "/> <property name =" shared "value =" true "> </property> <! -- This is the key !!! No required error --> </bean> <! -- End MyBatis uses ehcache cache -->
<! -- The cache manager is implemented using Ehcache --> <bean id = "shiroCacheManager" class = "org. apache. shiro. cache. ehcache. ehCacheManager "> <property name =" cacheManager "ref =" ehCacheManager "/> </bean>
4: Configure encache in your mapper. xml and add the following configurations.
<cache type="org.mybatis.caches.ehcache.LoggingEhcache" > <property name="timeToIdleSeconds" value="3600"/> <property name="timeToLiveSeconds" value="3600"/> <property name="maxEntriesLocalHeap" value="1000"/> <property name="maxEntriesLocalDisk" value="10000000"/> <property name="memoryStoreEvictionPolicy" value="LRU"/> </cache>
LoggingEhcache prints logs.EhcacheCache
Finally, the operations in mapper. xml are global and the default value is useCache = "true,
If a service is not cached, you can add useCache = "false" to the current service"
<select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String" useCache="false">
--------- In this blog, the author adds his own original application based on internet information, which can be reproduced but the source must be explained --------