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 在查詢時,會先在緩存中查找數據,當緩存中數據不存在時,纔會執行之後方法查找數據庫