基于spring 方法級緩存的多種實現(xiàn)
方案實施
1、 spring和ehcache集成
主要獲取ehcache作為操作ehcache的對象。
spring.xml中注入ehcacheManager和ehCache對象,ehcacheManager是需要加載ehcache.xml配置信息,創(chuàng)建ehcache.xml中配置不同策略的cache。
<!-- ehCache 配置管理器 --> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> <!--true:單例,一個cacheManager對象共享;false:多個對象獨(dú)立 --> <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和自帶的緩存支持
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap實現(xiàn)的緩存管理器(該功能是從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服務(wù)器地址
redis.ip=127.0.0.1
#Port
redis.port=6379
#Passord
#redis.password=123456
#連接超時30000
redis.timeout=30
#最大分配的對象數(shù)
redis.pool.maxActive=100
#最大能夠保持idel狀態(tài)的對象數(shù)
redis.pool.maxIdle=30
#當(dāng)池內(nèi)沒有返回對象時,最大等待時間
redis.pool.maxWait=1000
#當(dāng)調(diào)用borrow Object方法時,是否進(jìn)行有效性檢查
redis.pool.testOnBorrow=true
#當(dāng)調(diào)用return Object方法時,是否進(jìn)行有效性檢查
redis.pool.testOnReturn=true
spring注入jedisPool、redisConnFactory、redisTemplate對象
<!-- 加載redis.propertis --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:redis.properties"/> </bean>
<!-- 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>
<!-- 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>
<!-- redis 操作對象 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisConnFactory" /> </bean>
<!-- 自定義緩存 --> <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>
<!-- 啟用緩存注解功能(請將其配置在Spring主配置文件中) --> <cache:annotation-driven cache-manager="cacheManager"/>
4.spring 緩存注解解釋
緩存注解有以下三個:
@Cacheable @CacheEvict @CachePut
1.
@Cacheable(value=”accountCache”),這個注釋的意思是,當(dāng)調(diào)用這個方法的時候,會從一個名叫 accountCache 的緩存中查詢,如果沒有,則執(zhí)行實際的方法,并將執(zhí)行的結(jié)果存入緩存中,否則返回緩存中的對象。這里的緩存中的 key 就是參數(shù) userName,value 就是 Account 對象?!癮ccountCache”緩存是在 spring*.xml 中定義的名稱。
例子:
@Cacheable(value="accountCache")// 使用了一個緩存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法內(nèi)部實現(xiàn)不考慮緩存邏輯,直接實現(xiàn)業(yè)務(wù)
System.out.println("real query account."+userName);
return getFromDB(userName);
}
condition:用來條件判斷,滿足條件的則進(jìn)行緩存
例子2:
@Cacheable(value="accountCache",condition="#userName.length() <=4")// 緩存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法內(nèi)部實現(xiàn)不考慮緩存邏輯,直接實現(xiàn)業(yè)務(wù)
return getFromDB(userName);
}
2.
@CacheEvict 注釋來標(biāo)記要清空緩存的方法,當(dāng)這個方法被調(diào)用后,即會清空緩存。注意其中一個 @CacheEvict(value=”accountCache”,key=”#account.getName()”),其中的 Key 是用來指定緩存的 key 的,這里因為我們保存的時候用的是 account 對象的 name 字段,所以這里還需要從參數(shù) account 對象中獲取 name 的值來作為 key,前面的 # 號代表這是一個 SpEL 表達(dá)式,此表達(dá)式可以遍歷方法的參數(shù)對象
例子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) {
// 方法內(nèi)部實現(xiàn)不考慮緩存邏輯,直接實現(xiàn)業(yè)務(wù)
return getFromDB(userName);
}
3.
@CachePut 注釋,這個注釋可以確保方法被執(zhí)行,同時方法的返回值也被記錄到緩存中,實現(xiàn)緩存與數(shù)據(jù)庫的同步更新。
@CachePut(value="accountCache",key="#account.getName()")// 更新accountCache 緩存
public Account updateAccount(Account account) {
return updateDB(account);
}
附錄:
@Cacheable、@CachePut、@CacheEvict 注釋介紹
通過上面的例子,我們可以看到 spring cache 主要使用兩個注釋標(biāo)簽,即 @Cacheable、@CachePut 和 @CacheEvict,我們總結(jié)一下其作用和配置方法。
表 1. @Cacheable 作用和配置方法
@Cacheable 的作用 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進(jìn)行緩存
@Cacheable 主要的參數(shù) |
||
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 |
例如: @Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
key |
緩存的 key,可以為空,如果指定要按照 SpEL 表達(dá)式編寫,如果不指定,則缺省按照方法的所有參數(shù)進(jìn)行組合 |
例如: @Cacheable(value=”testcache”,key=”#userName”) |
condition |
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進(jìn)行緩存 |
例如: @Cacheable(value=”testcache”,condition=”#userName.length()>2”) |
表 2. @CachePut 作用和配置方法
@CachePut 的作用 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進(jìn)行緩存,和 @Cacheable 不同的是,它每次都會觸發(fā)真實方法的調(diào)用
@CachePut 主要的參數(shù) |
||
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 |
例如: @Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
key |
緩存的 key,可以為空,如果指定要按照 SpEL 表達(dá)式編寫,如果不指定,則缺省按照方法的所有參數(shù)進(jìn)行組合 |
例如: @Cacheable(value=”testcache”,key=”#userName”) |
condition |
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進(jìn)行緩存 |
例如: @Cacheable(value=”testcache”,condition=”#userName.length()>2”) |
表 3. @CacheEvict 作用和配置方法
@CachEvict 的作用 主要針對方法配置,能夠根據(jù)一定的條件對緩存進(jìn)行清空
@CacheEvict 主要的參數(shù) |
||
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 |
例如: @CachEvict(value=”mycache”) 或者 @CachEvict(value={”cache1”,”cache2”} |
key |
緩存的 key,可以為空,如果指定要按照 SpEL 表達(dá)式編寫,如果不指定,則缺省按照方法的所有參數(shù)進(jìn)行組合 |
例如: @CachEvict(value=”testcache”,key=”#userName”) |
condition |
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才清空緩存 |
例如: @CachEvict(value=”testcache”, condition=”#userName.length()>2”) |
allEntries |
是否清空所有緩存內(nèi)容,缺省為 false,如果指定為 true,則方法調(diào)用后將立即清空所有緩存 |
例如: @CachEvict(value=”testcache”,allEntries=true) |
beforeInvocation |
是否在方法執(zhí)行前就清空,缺省為 false,如果指定為 true,則在方法還沒有執(zhí)行的時候就清空緩存,缺省情況下,如果方法執(zhí)行拋出異常,則不會清空緩存 |
例如: @CachEvict(value=”testcache”,beforeInvocation=true) |
以上這篇基于spring 方法級緩存的多種實現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java利用位運(yùn)算實現(xiàn)比較兩個數(shù)的大小
這篇文章主要為大家介紹了,在Java中如何不用任何比較判斷符(>,==,<),返回兩個數(shù)( 32 位整數(shù))中較大的數(shù),感興趣的可以了解一下2022-08-08Spring依賴注入多種類型數(shù)據(jù)的示例代碼
這篇文章主要介紹了Spring依賴注入多種類型數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03JavaWeb?Servlet實現(xiàn)文件上傳與下載功能實例
因自己負(fù)責(zé)的項目中需要實現(xiàn)文件上傳,所以下面下面這篇文章主要給大家介紹了關(guān)于JavaWeb?Servlet實現(xiàn)文件上傳與下載功能的相關(guān)資料,需要的朋友可以參考下2022-04-04JAVA 格式化JSON數(shù)據(jù)并保存到j(luò)son文件中的實例
這篇文章主要介紹了JAVA 格式化JSON數(shù)據(jù)并保存到j(luò)son文件中的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10