所謂緩存,就是將程序或系統經常要調用的對象存在內存中,以便其使用時可以快速調用,不必再去創建新的重復的實例。這樣做可以減少系統開銷,提高系統效率。
緩存主要可分為二大類:
一、通過文件緩存,顧名思義文件緩存是指把數據存儲在磁盤上,不管你是以XML格式,序列化文件DAT格式還是其它文件格式;
二、內存緩存,也就是實現一個類中靜態Map,對這個Map進行常規的增刪查.
一、EhCache緩存系統簡介
EhCache 是一個純 Java 的進程內緩存框架,具有快速、精干等特點,是 Hibernate 中默認的 CacheProvider。
EhCache 應用架構圖,下圖是 EhCache 在應用程序中的位置:
EhCache 的主要特性有:
1. 快速、精干; 2. 簡單; 3. 多種緩存策略; 4. 緩存數據有兩級:內存和磁盤,因此無需擔心容量問題; 5. 緩存數據會在虛擬機重啟的過程中寫入磁盤; 6. 可以通過 RMI、可插入 API 等方式進行分布式緩存; 7. 具有緩存和緩存管理器的偵聽接口; 8. 支持多緩存管理器實例,以及一個實例的多個緩存區域; 9. 提供 Hibernate 的緩存實現;
二、maven添加Ehcache核心包
在pom.xml配置文件里,添加
<!--ehcache 相關包 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency>
三、添加配置文件
在資源文件夾下(通常是src/main/resources/META-INF) 添加 applicationContext-ehcache.xml、ehcache.xml
1、applicationContext-ehcache.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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 開啟spring緩存 --> <cache:annotation-driven cache-manager="cacheManager" /> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:META-INF/ehcache.xml"/> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean> </beans>
這里是開啟spring緩存
2、ehcache.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true"> <diskStore path="${java.io.tmpdir}/${system.project_name}/cache" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="baseCache" maxElementsInMemory="200" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /> </ehcache>
這里是添加緩存策略
說明:
1、java.io.tmpdir 所指地址:
操作系統不同 這個系統屬性所表示的目錄也不同 On Windows: java.io.tmpdir:[C:\DOCUME~1\joshua\LOCALS~1\Temp\] On Solaris: java.io.tmpdir:[/var/tmp/] On Linux: java.io.tmpdir: [/tmp] On Mac OS X: java.io.tmpdir: [/tmp] The default temporary-file directory is specified by the system property java.io.tmpdir. On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "c:\temp". A different value may be given to this system property when the Java virtual machine is invoked, but programmatic changes to this property are not guaranteed to have any effect upon the the temporary directory used by this method. To specify the java.io.tmpdir System property, you can invoke the JVM as follows: java -Djava.io.tmpdir=/path/to/tmpdir By default this value should come from the TMP environment variable on Windows systems
2、數據含義
name:Cache的唯一標識
maxElementsInMemory:內存中最大緩存對象數
maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大
eternal:Element是否永久有效,一但設置了,timeout將不起作用
overflowToDisk:配置此屬性,當內存中Element數量達到maxElementsInMemory時,Ehcache將會Element寫到磁盤中
timeToIdleSeconds:設置Element在失效前的允許閑置時間。僅當element不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大
timeToLiveSeconds:設置Element在失效前允許存活時間。最大時間介於創建時間和失效時間之間。僅當element不是永久有效時使用,默認是0.,也就是element存活時間無窮大
diskPersistent:是否緩存虛擬機重啟期數據
diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒
diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)
或是LFU(較少使用)
四、DAO層做配置
import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.Cacheable; ..... @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(value="baseCache", key = "'findAll'") public List<Entity> findAll() throws SearchException { return list; }
@Cacheable(value="baseCache", key = "'findAll'")
這個注解就是做到緩存數據,cacheName對應ehcache.xml 中配置
@TriggersRemove(cacheName="baseCache",removeAll=true)
這個注解的作用就是當數據發生變化的時候清除緩存,做到數據同步
擴展:@Cacheable可以指定三個屬性,value、key和condition。
1、@Cacheable("cache1")、@Cacheable({"cache1", "cache2"})//Cache是發生在cache1和cache2上的
value屬性是必須指定的,其表示當前方法的返回值是會被緩存在哪個Cache上的,對應Cache的名稱。其可以是一個Cache也可以是多個Cache,當需要指定多個Cache時其是一個數組。
2、@Cacheable(value="users", key="#user.id")、@Cacheable(value="users", key="#p0")
key屬性是用來指定Spring緩存方法的返回結果時對應的key的。該屬性支持SpringEL表達式。當我們沒有指定該屬性時,Spring將使用默認策略生成key。我們這里先來看看自定義策略,自定義策略是指我們可以通過Spring的EL表達式來指定我們的key。這里的EL表達式可以使用方法參數及它們對應的屬性
3、@Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
有的時候我們可能並不希望緩存一個方法所有的返回結果。通過condition屬性可以實現這一功能。condition屬性默認為空,表示將緩存所有的調用情形。其值是通過SpringEL表達式來指定的,當為true時表示進行緩存處理;當為false時表示不進行緩存處理,即每次調用該方法時該方法都會執行一次。以上下示例表示只有當user的id為偶數時才會進行緩存。
4、@CachePut("users")
在支持Spring Cache的環境下,對於使用@Cacheable標注的方法,Spring在每次執行前都會檢查Cache中是否存在相同key的緩存元素,如果存在就不再執行該方法,而是直接從緩存中獲取結果進行返回,否則才會執行並將返回結果存入指定的緩存中。@CachePut也可以聲明一個方法支持緩存功能。與@Cacheable不同的是使用@CachePut標注的方法在執行前不會去檢查緩存中是否存在之前執行過的結果,而是每次都會執行該方法,並將執行結果以鍵值對的形式存入指定的緩存中。
@CachePut也可以標注在類上和方法上。使用@CachePut時我們可以指定的屬性跟@Cacheable是一樣的。
5、@CacheEvict(value="users", allEntries=true)
allEntries是boolean類型,表示是否需要清除緩存中的所有元素。默認為false,表示不需要。當指定了allEntries為true時,Spring Cache將忽略指定的key。有的時候我們需要Cache一下清除所有的元素,這比一個一個清除元素更有效率。
6、@CacheEvict(value="users", beforeInvocation=true)
清除操作默認是在對應方法成功執行之后觸發的,即方法如果因為拋出異常而未能成功返回時也不會觸發清除操作。使用beforeInvocation可以改變觸發清除操作的時間,當我們指定該屬性值為true時,Spring會在調用該方法之前清除緩存中的指定元素。
7、@Caching(cacheable = @Cacheable("users"), evict = { @CacheEvict("cache2"),
@CacheEvict(value = "cache3", allEntries = true) })
public User find(Integer id) {
return null;
}
@Caching注解可以讓我們在一個方法或者類上同時指定多個Spring Cache相關的注解。其擁有三個屬性:cacheable、put和evict,分別用於指定@Cacheable、@CachePut和@CacheEvict。
如何知道有沒生效:查看sql執行日志
參考:
http://www.blogjava.net/zzzlyr/articles/343234.html
http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/