spring +ehcache 註解方式配置快取(spring與googlecode 的ehcache 兩種)
前言:有人知道 -spring +ehcache 註解方式和mybatis +ehcache的區別嗎?
(①spring的ehcache與②googlecode 的ehcache)//這是兩種配置,都能實現ehcache快取。綠色表示1需要,藍色表示2需要 黑色表示都需要
google的ehcache配置更簡單,它為spring與ehcache整合提供了封裝包
一、匯入所需要的包 ehcache-core-2.4.4.jar ②ehcache-spring-annotations-1.2.0.jar guava-r09.jar(slf4j-api-1.6.2.jar和
slf4j-log4j12-1.6.2.jar也要
googlecode 的ehcache必須依賴於ehcache-spring-annotations-1.2.0.jar guava-r09.jar
二、專案目錄先看下:
三、新建 ehcache.xml(兩種配置這個檔案相同)
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- java.io.tmpdir:Java臨時目錄。指定一個檔案目錄,當EhCache把資料寫到硬碟上或者系統jvm記憶體時,將把資料寫到這個檔案目錄下 -->
<diskStore path="java.io.tmpdir"/>
<!-- maxElementsInMemory:設定基於記憶體的快取可存放物件的最大數目。 -->
<!-- eternal:如果為true,表示物件永遠不會過期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,預設為false; -->
<!-- timeToIdleSeconds: 設定允許物件處於空閒狀態的最長時間,以秒為單位。當物件自從最近一次被訪問後,如果處於空閒狀態的時間超過了 -->
<!-- timeToIdleSeconds屬性值,這個物件就會過期。當物件過期,EHCache將把它從快取中清空。只有當eternal屬性為false,該屬性才有效。 -->
<!-- 如果該屬性值為0,則表示物件可以無限期地處於空閒狀態。 -->
<!-- timeToLiveSeconds:設定物件允許存在於快取中的最長時間,以秒為單位。當物件自從被存放到快取中後,如果處於快取中的時間超過了 timeToLiveSeconds屬性值,這個物件就會過期。當物件過期,EHCache將把它從快取中清除。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示物件可以無限期地存在於快取中。timeToLiveSeconds必須大於timeToIdleSeconds屬性,才有意義。 -->
<!-- overflowToDisk:如果為true,表示當基於記憶體的快取中的物件數目達到了maxElementsInMemory界限後,會把益出的物件寫到基於硬碟的快取中。 -->
<!-- 設定快取的預設資料過期策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="20"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
<!-- 自定義快取策略-學生資訊快取容器對應策略-->
<cache name="userCache" // 註解時候@Cacheable(value="userCache") 和這個name一致
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="20"/>
<!-- 自定義快取策略-教師資訊快取容器對應策略-->
<!-- <cache name="techerCache" -->
<!-- maxElementsInMemory="1000" -->
<!-- eternal="false" -->
<!-- overflowToDisk="true" -->
<!-- timeToIdleSeconds="10" -->
<!-- timeToLiveSeconds="20"/> -->
</ehcache>
四、新建 applicationContext-echache.xml(下面紅色部分別忘了加)
<?xml version="1.0" encoding="UTF-8"?>
<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"
// 如果沒上面這句會報錯:元素 "cache:annotation-driven" 的字首 "cache" 未繫結。
xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<!--spring的ehcache -->
<!--①的 快取配置 -->
<!-- 啟用快取註解功能(請將其配置在Spring主配置檔案中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基於java.util.concurrent.ConcurrentHashMap實現的快取管理器(該功能是從Spring3.1開始提供的) -->
<!--
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
-->
<!-- 若只想使用Spring自身提供的快取器,則註釋掉下面的兩個關於Ehcache配置的bean,並啟用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基於的Ehcache實現的快取管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/ehcache.xml"/><!--//注意這個路徑-->
<property name="shared" value="true"/> <!-- 這裡是關鍵!!!沒有必錯 -->
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基於java.util.concurrent.ConcurrentHashMap實現的快取管理器(該功能是從Spring3.1開始提供的) -->
<!--
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
-->
<!-- 若只想使用Spring自身提供的快取器,則註釋掉下面的兩個關於Ehcache配置的bean,並啟用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基於的Ehcache實現的快取管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/ehcache.xml"/><!--//注意這個路徑-->
<property name="shared" value="true"/> <!-- 這裡是關鍵!!!沒有必錯 -->
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
<!--googlecode 的ehcache -->
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="/WEB-INF/ehcache.xml"/>
</bean>
</beans>
五、在spring-mvc.xml中匯入applicationContext-echache.xml
xml中加上 <!--載入ehcache快取-->
<import resource="applicationContext-echache.xml"/> <!--//注意這個路徑-->
六、測試類:
*controoler.java:
@RequestMapping("findUserList")
public String findUserList(HttpServletRequest request, Model model) {User user1 = (User) request.getSession().getAttribute("user");
List<User> user = userService.findUserList(user1.getName()); //第一次執行sql查詢 在該方法上面配置如下
System.out.println("----------------------------start");
List<User> user22 = userService.findUserList(user1.getName()); //第二次執行快取中獲取
System.out.println("----------------------------end");
model.addAttribute("userList", user);
return "user/userInformation";
}
*service.java
import org.springframework.cache.annotation.Cacheable;
import com.googlecode.ehcache.annotations.Cacheable;
@Cacheable(value="userCache") 和ehcache中<cache name="userCache" 相同
@Cacheable(cacheName="userCache")
// TODO Auto-generated method stub
return iUserMapper.findUserList(name);
}
七、執行列印日誌如下
2018-06-14 21:00:39,354 [http-bio-8080-exec-4] DEBUG [com.user.dao.IUserMapper.findUserList] - ==> Preparing: select a.* from users a where a.name not in (?) //第一次執行
2018-06-14 21:00:39,354 [http-bio-8080-exec-4] DEBUG [com.user.dao.IUserMapper.findUserList] - ==> Parameters: 2(String)
2018-06-14 21:00:39,358 [http-bio-8080-exec-4] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [[email protected]]
----------------------------start//第二次執行
----------------------------end
八、在摸索階段自己犯了很多錯誤,一直不能成功。現在雖然成功,但是還有很多關於它的疑問。難受難受!!!