Spring MVC3 + Ehcache 緩存實現

版本:Spring3.0.6

準備工作:

下載 ehcache-spring-annotations-1.2.0 http://code.google.com/p/ehcache-spring-annotations/downloads/list 

下載完加壓后里面的lib下的jar統統添加到classpath中

在資源文件夾下(通常是src/main/resources) 添加 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"
	updateCheck="false">
	
	<diskStore path="java.io.tmpdir" />
	
	<defaultCache eternal="false" 
		maxElementsInMemory="1000"
		overflowToDisk="false" 
		diskPersistent="false" 
		timeToIdleSeconds="0"
		timeToLiveSeconds="600" 
		memoryStoreEvictionPolicy="LFU" />

	<cache name="baseCache" 
		eternal="false" 
		maxElementsInMemory="500"
		overflowToDisk="false" 
		diskPersistent="false" 
		timeToIdleSeconds="0"
		timeToLiveSeconds="300" 
		memoryStoreEvictionPolicy="LFU" />

</ehcache>

這裏是定義緩存策略

eternal="false"   // 元素是否永恆,如果是就永不過期(必須設置)
  maxElementsInMemory="1000" // 緩存容量的內存最大值(必須設置)
  overflowToDisk="false"  // 當緩存達到maxElementsInMemory值是,是否允許溢出到磁盤(必須設置)
  diskPersistent="false"  // 磁盤緩存在VM重新啓動時是否保持(默認爲false)
  timeToIdleSeconds="0" // 導致元素過期的訪問間隔(秒爲單位). 0表示可以永遠空閒,默認爲0
  timeToLiveSeconds="600" // 元素在緩存裏存在的時間(秒爲單位). 0 表示永遠存在不過期
  memoryStoreEvictionPolicy="LFU" // 當達到maxElementsInMemory時,如何強制進行驅逐默認使用"最近使用(LRU)"策略,其它還有先入先出FIFO,最少使用LFU,較少使用LRU

 

然後在添加 cache-config.xml 內容如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring   
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

	<ehcache:annotation-driven />

	<ehcache:config cache-manager="cacheManager">
		<ehcache:evict-expired-elements
			interval="60" />
	</ehcache:config>

	<bean id="cacheManager"
		class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="classpath:ehcache.xml" />
	</bean>

</beans>

然後在DAO層做如下配置:

         @TriggersRemove(cacheName="baseCache",removeAll=true)
	public Entity save(Entity entity) throws CrudException {
		return entity;
	}

	@TriggersRemove(cacheName="baseCache",removeAll=true)
	public Entity update(Entity entity) throws CrudException {
		return entity;
	}

	@TriggersRemove(cacheName="baseCache",removeAll=true)
	public void del(Entity entity) throws CrudException {
			}

	@Cacheable(cacheName="baseCache")	@SuppressWarnings("unchecked")
	public List<Entity> findAll() throws SearchException {
		return list;
	}

@Cacheable(cacheName="baseCache")

這個註解就是做到緩存數據,cacheName對應ehcache.xml 中配置


 @TriggersRemove(cacheName="baseCache",removeAll=true)

這個註解的作用就是當數據發生變化的時候清除緩存,做到數據同步

 

如何知道有沒生效:

新建一個單元測試,在類中添加兩個搜索的方法,然後執行這個單元測試,然後查看兩個方法執行的時間(Eclipse的單元測試可以查看)

如果第二個搜索方法的運行時間爲0那就說嗎成功了,如圖

 

參考:

http://www.blogjava.net/zzzlyr/articles/343234.html

http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/  (英文的,推薦)

本文轉自:http://blog.csdn.net/thc1987/article/details/7345816

感謝分享!









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章