方案實施
1、 spring和ehcache集成
主要獲取ehcache作為操作ehcache的對象。
spring.xml中注入ehcacheManager和ehCache對象,ehcacheManager是需要加載ehcache.xml配置信息,創建ehcache.xml中配置不同策略的cache。
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- ehCache 配置管理器 --> < bean id = "ehcacheManager" class = "org.springframework.cache.ehcache.EhCacheManagerFactoryBean" > < property name = "configLocation" value = "classpath:ehcache.xml" /> <!--true:單例,一個cacheManager對象共享;false:多個對象獨立 --> < property name = "shared" value = "true" /> < property name = "cacheManagerName" value = "ehcacheManager" /> </ bean > <!-- ehCache 操作對象 --> < bean id = "cacheManager" class = "org.springframework.cache.ehcache.EhCacheCacheManager" > < property name = "cacheManager" ref = "ehcacheManager" /> </ bean > <!-- 啟用緩存注解功能(請將其配置在Spring主配置文件中) --> < cache:annotation-driven cache-manager = "cacheManager" /> |
2、 spring和自帶的緩存支持
1
2
3
4
5
6
7
8
|
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap實現的緩存管理器(該功能是從Spring3.1開始提供的) --> < bean id = "cacheManager" class = "org.springframework.cache.support.SimpleCacheManager" > < property name = "caches" > < set > < bean name = "SimplePageCachingFilter" class = "org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" /> </ set > </ property > </ bean > |
3.spring和redis集成
主要獲取redisTemplate作為操作redis的對象。
redis.properties配置信息
#host 寫入redis服務器地址
redis.ip=127.0.0.1
#Port
redis.port=6379
#Passord
#redis.password=123456
#連接超時30000
redis.timeout=30
#最大分配的對象數
redis.pool.maxActive=100
#最大能夠保持idel狀態的對象數
redis.pool.maxIdle=30
#當池內沒有返回對象時,最大等待時間
redis.pool.maxWait=1000
#當調用borrow Object方法時,是否進行有效性檢查
redis.pool.testOnBorrow=true
#當調用return Object方法時,是否進行有效性檢查
redis.pool.testOnReturn=true
spring注入jedisPool、redisConnFactory、redisTemplate對象
1
2
3
4
5
6
7
|
<!-- 加載redis.propertis --> < bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > < property name = "locations" value = "classpath:redis.properties" /> </ bean > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- Redis 連接池 --> < bean id = "jedisPool" class = "redis.clients.jedis.JedisPoolConfig" > < property name = "maxTotal" value = "${redis.pool.maxActive}" /> < property name = "maxIdle" value = "${redis.pool.maxIdle}" /> < property name = "testOnBorrow" value = "${redis.pool.testOnBorrow}" /> < property name = "testOnReturn" value = "${redis.pool.testOnReturn}" /> < property name = "maxWaitMillis" value = "${redis.pool.maxWait}" /> </ bean > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- Redis 連接工廠 --> < bean id = "redisConnFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > < property name = "hostName" value = "${redis.ip}" /> < property name = "port" value = "${redis.port}" /> <!-- property name="password" value="${redis.password}" --> < property name = "timeout" value = "${redis.timeout}" /> < property name = "poolConfig" ref = "jedisPool" /> </ bean > |
1
2
3
4
5
6
7
|
<!-- redis 操作對象 --> < bean id = "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate" > < property name = "connectionFactory" ref = "redisConnFactory" /> </ bean > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!-- 自定義緩存 --> < bean id = "cacheManager" class = "org.springframework.cache.support.SimpleCacheManager" > < property name = "caches" > < set > < bean class = "org.cpframework.cache.redis.RedisCache" > < property name = "redisTemplate" ref = "redisTemplate" /> < property name = "name" value = "default" /> </ bean > </ set > </ property > </ bean > |
1
2
3
|
<!-- 啟用緩存注解功能(請將其配置在Spring主配置文件中) --> < cache:annotation-driven cache-manager = "cacheManager" /> |
4.spring 緩存注解解釋
緩存注解有以下三個:
@Cacheable @CacheEvict @CachePut
1.
@Cacheable(value=”accountCache”),這個注釋的意思是,當調用這個方法的時候,會從一個名叫 accountCache 的緩存中查詢,如果沒有,則執行實際的方法,并將執行的結果存入緩存中,否則返回緩存中的對象。這里的緩存中的 key 就是參數 userName,value 就是 Account 對象。“accountCache”緩存是在 spring*.xml 中定義的名稱。
例子:
@Cacheable(value="accountCache")// 使用了一個緩存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法內部實現不考慮緩存邏輯,直接實現業務
System.out.println("real query account."+userName);
return getFromDB(userName);
}
condition:用來條件判斷,滿足條件的則進行緩存
例子2:
@Cacheable(value="accountCache",condition="#userName.length() <=4")// 緩存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法內部實現不考慮緩存邏輯,直接實現業務
return getFromDB(userName);
}
2.
@CacheEvict 注釋來標記要清空緩存的方法,當這個方法被調用后,即會清空緩存。注意其中一個 @CacheEvict(value=”accountCache”,key=”#account.getName()”),其中的 Key 是用來指定緩存的 key 的,這里因為我們保存的時候用的是 account 對象的 name 字段,所以這里還需要從參數 account 對象中獲取 name 的值來作為 key,前面的 # 號代表這是一個 SpEL 表達式,此表達式可以遍歷方法的參數對象
例子3:
@CacheEvict(value="accountCache",key="#account.getName()")// 清空accountCache 緩存
public void updateAccount(Account account) {
updateDB(account);
}
@CacheEvict(value="accountCache",allEntries=true)// 清空accountCache 緩存
public void reload() {
reloadAll()
}
@Cacheable(value="accountCache",condition="#userName.length() <=4")// 緩存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法內部實現不考慮緩存邏輯,直接實現業務
return getFromDB(userName);
}
3.
@CachePut 注釋,這個注釋可以確保方法被執行,同時方法的返回值也被記錄到緩存中,實現緩存與數據庫的同步更新。
@CachePut(value="accountCache",key="#account.getName()")// 更新accountCache 緩存
public Account updateAccount(Account account) {
return updateDB(account);
}
附錄:
@Cacheable、@CachePut、@CacheEvict 注釋介紹
通過上面的例子,我們可以看到 spring cache 主要使用兩個注釋標簽,即 @Cacheable、@CachePut 和 @CacheEvict,我們總結一下其作用和配置方法。
表 1. @Cacheable 作用和配置方法
@Cacheable 的作用 主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存
@Cacheable 主要的參數 |
||
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 |
例如: @Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
key |
緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合 |
例如: @Cacheable(value=”testcache”,key=”#userName”) |
condition |
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存 |
例如: @Cacheable(value=”testcache”,condition=”#userName.length()>2”) |
表 2. @CachePut 作用和配置方法
@CachePut 的作用 主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存,和 @Cacheable 不同的是,它每次都會觸發真實方法的調用
@CachePut 主要的參數 |
||
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 |
例如: @Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
key |
緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合 |
例如: @Cacheable(value=”testcache”,key=”#userName”) |
condition |
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存 |
例如: @Cacheable(value=”testcache”,condition=”#userName.length()>2”) |
表 3. @CacheEvict 作用和配置方法
@CachEvict 的作用 主要針對方法配置,能夠根據一定的條件對緩存進行清空
@CacheEvict 主要的參數 |
||
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 |
例如: @CachEvict(value=”mycache”) 或者 @CachEvict(value={”cache1”,”cache2”} |
key |
緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合 |
例如: @CachEvict(value=”testcache”,key=”#userName”) |
condition |
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才清空緩存 |
例如: @CachEvict(value=”testcache”, condition=”#userName.length()>2”) |
allEntries |
是否清空所有緩存內容,缺省為 false,如果指定為 true,則方法調用后將立即清空所有緩存 |
例如: @CachEvict(value=”testcache”,allEntries=true) |
beforeInvocation |
是否在方法執行前就清空,缺省為 false,如果指定為 true,則在方法還沒有執行的時候就清空緩存,缺省情況下,如果方法執行拋出異常,則不會清空緩存 |
例如: @CachEvict(value=”testcache”,beforeInvocation=true) |
以上這篇基于spring 方法級緩存的多種實現就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/liuzhuqing/p/7552747.html