mybatis + ehcache 配置
1、引入ehcache jar包,pom.xml添加
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
2、添加ehcache配置文件,ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 配置缓存文件的路劲
java.io.tmpdir,表示临时文件夹,windows表示在C:\Documents and Settings\Administrator\Local Setting\Temp
-->
<diskStore path="../temp/ehcache"/>
<!-- 设定缓存的默认数据过期策略 -->
<!--
name:缓存名称
maxElementsInMemory:缓存最大个数
eternal:对象是否永久有效,一但设置了,timeout将不起作用
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒),仅当eternal=false对象不是永久有效时使用
timeToLiveSeconds:设置对象在失效前允许存活时间,最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB
maxElementsOnDisk:硬盘最大缓存个数
diskPersistent: 是否缓存虚拟机重启期数据
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU。
最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU
clearOnFlush:内存数量最大时是否清除
-->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
<cache name="userCache"
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
3、添加ehcache-config.xml,springmvc集成cache
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd ">
<!-- 应用spring cache注解功能 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- 配置ehcache管理器-->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
<property name="transactionAware" value="true"/>
</bean>
</beans>
4、将ehcache-config.xml引入spring配置文件中
<!-- 添加ehcache配置文件 -->
<import resource="ehcache-config.xml" />
5、给需要启用ehcache的sql方法添加启用配置
import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
public interface UserMapper {
int insertSelective(User record);
@Cacheable(value = "userCache")
User selectByPrimaryKey(Long id);
@CacheEvict(value = "userCache", allEntries = true)
int updateByPrimaryKeySelective(User record);
@CacheEvict(value = "userCache", allEntries = true)
int updateByPrimaryKeysSelective(@Param("record") User record, @Param("ids") Collection<Long> ids);
/**
* 删除
*
* @param record
*/
@CacheEvict(value = "userCache", allEntries = true)
void deleteUser(User record);
}
@CacheEvict 在执行增删改操作时,为了保证缓存和数据库信息一致性,会将缓存信息清空 @Cacheable 在查询时,会先在缓存中查找数据,当缓存中数据不存在时,才会执行之后方法查找数据库