<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.1</version>
</dependency>java
<?xml version="1.0" encoding="UTF-8"?> <ehcache updateCheck="false"> <diskStore path="java.io.tmpdir"/> <!-- name:緩存名稱。 maxElementsInMemory:緩存最大個數。 eternal:對象是否永久有效,一但設置了,timeout將不起做用。 timeToIdleSeconds:設置對象在失效前的容許閒置時間(單位:秒)。 僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大。 timeToLiveSeconds:設置對象在失效前容許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。 僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。 overflowToDisk:當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。 diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每一個Cache都應該有本身的一個緩衝區。 maxElementsOnDisk:硬盤最大緩存個數。 diskPersistent:是否緩存虛擬機重啓期數據 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。 memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。 默認策略是LRU(最近最少使用)。你能夠設置爲FIFO(先進先出)或是LFU(較少使用)。 clearOnFlush:內存數量最大時是否清除。 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="60" timeToLiveSeconds="60" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="QY_api_productTop" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="20" timeToLiveSeconds="20" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="1"/> </ehcache>
<beans
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"
>spring
<!-- 啓用緩存註解功能,這個是必須的,不然註解不會生效,另外,該註解必定要聲明在spring主配置文件中才會生效 -->
<cache:annotation-driven cache-manager="ehcacheManager"/>
<!-- cacheManager工廠類,指定ehcache.xml的位置 -->
<bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- 聲明cacheManager -->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManagerFactory" />
</bean>
sql
@Cacheableapi
Cache a method call. In the following example, the value is the return type, a Manual. The key is extracted from the ISBN argument using the id.緩存
@org.springframework.cache.annotation.Cacheable(value="QY_api_productTop", key="#isbn.id") public Manual findManual(ISBN isbn, boolean checkWarehouse)
key="#isbn.id" 對象緩存的key值,須要保證惟一, 用的是Spring的 SpEL表達式, 取值爲 isbn對象的id屬性值app
value="QY_api_productTop":指的是ehcache.xml裏的緩存名字測試
還支持條件condition,包括 屬性 id<10,以下:ui
@Cacheable(value = "QY_api_productTop",key="#isbn.id",condition = "#isbn.id<10") public Manual findManual(ISBN isbn, boolean checkWarehouse)
@CacheEvict
this
Clears the cache when called. 清除QY_api_productTop緩存中全部對象google
@org.springframework.cache.annotation.CacheEvict(value = "QY_api_productTop", allEntries=true) public void loadManuals(Long id)
能夠清除指定對象的緩存例如:
@CacheEvict(value = "QY_api_productTop", key = "#id") public void loadManuals(Long id)
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.1.2</version>
</dependency>
This open source, led by Eric Dalquist, predates the Spring 3.1 project. You can use it with earlier versions of Spring, or you can use it with 3.1.
@Cacheable
As with Spring 3.1 it uses an @Cacheable annotation to cache a method. In this example calls to findMessage are stored in a cache named 「messageCache」. The values are of type Message. The id for each entry is the id argument given.
@Cacheable(cacheName = "messageCache")
public Message findMessage(long id)
@TriggersRemove
And for cache invalidation, there is the @TriggersRemove annotation. In this example, cache.removeAll() is called after the method is invoked.
@TriggersRemove(cacheName = "messagesCache",
when = When.AFTER_METHOD_INVOCATION, removeAll = true)
public void addMessage(Message message)
例如hibernate配置中設置show_sql=true
<prop key="hibernate.show_sql">true</prop>
5.1 第一次訪問緩存方法後在緩存時間內屢次訪問查看是否有sql輸出 , 沒有爲緩存正常
5.2 第二次輸出時間和第一次時間間隔是否是大於 ehcache.xml中設置的時間 大於爲緩存正常