基于spring 方法級(jí)緩存的多種實(shí)現(xiàn)
方案實(shí)施
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:單例,一個(gè)cacheManager對象共享;false:多個(gè)對象獨(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實(shí)現(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
#連接超時(shí)30000
redis.timeout=30
#最大分配的對象數(shù)
redis.pool.maxActive=100
#最大能夠保持idel狀態(tài)的對象數(shù)
redis.pool.maxIdle=30
#當(dāng)池內(nèi)沒有返回對象時(shí),最大等待時(shí)間
redis.pool.maxWait=1000
#當(dāng)調(diào)用borrow Object方法時(shí),是否進(jìn)行有效性檢查
redis.pool.testOnBorrow=true
#當(dāng)調(diào)用return Object方法時(shí),是否進(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 緩存注解解釋
緩存注解有以下三個(gè):
@Cacheable @CacheEvict @CachePut
1.
@Cacheable(value=”accountCache”),這個(gè)注釋的意思是,當(dāng)調(diào)用這個(gè)方法的時(shí)候,會(huì)從一個(gè)名叫 accountCache 的緩存中查詢,如果沒有,則執(zhí)行實(shí)際的方法,并將執(zhí)行的結(jié)果存入緩存中,否則返回緩存中的對象。這里的緩存中的 key 就是參數(shù) userName,value 就是 Account 對象?!癮ccountCache”緩存是在 spring*.xml 中定義的名稱。
例子:
@Cacheable(value="accountCache")// 使用了一個(gè)緩存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法內(nèi)部實(shí)現(xiàn)不考慮緩存邏輯,直接實(shí)現(xiàn)業(yè)務(wù)
System.out.println("real query account."+userName);
return getFromDB(userName);
}
condition:用來?xiàng)l件判斷,滿足條件的則進(jìn)行緩存
例子2:
@Cacheable(value="accountCache",condition="#userName.length() <=4")// 緩存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法內(nèi)部實(shí)現(xiàn)不考慮緩存邏輯,直接實(shí)現(xiàn)業(yè)務(wù)
return getFromDB(userName);
}
2.
@CacheEvict 注釋來標(biāo)記要清空緩存的方法,當(dāng)這個(gè)方法被調(diào)用后,即會(huì)清空緩存。注意其中一個(gè) @CacheEvict(value=”accountCache”,key=”#account.getName()”),其中的 Key 是用來指定緩存的 key 的,這里因?yàn)槲覀儽4娴臅r(shí)候用的是 account 對象的 name 字段,所以這里還需要從參數(shù) account 對象中獲取 name 的值來作為 key,前面的 # 號(hào)代表這是一個(gè) 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)部實(shí)現(xiàn)不考慮緩存邏輯,直接實(shí)現(xiàn)業(yè)務(wù)
return getFromDB(userName);
}
3.
@CachePut 注釋,這個(gè)注釋可以確保方法被執(zhí)行,同時(shí)方法的返回值也被記錄到緩存中,實(shí)現(xiàn)緩存與數(shù)據(jù)庫的同步更新。
@CachePut(value="accountCache",key="#account.getName()")// 更新accountCache 緩存
public Account updateAccount(Account account) {
return updateDB(account);
}
附錄:
@Cacheable、@CachePut、@CacheEvict 注釋介紹
通過上面的例子,我們可以看到 spring cache 主要使用兩個(gè)注釋標(biāo)簽,即 @Cacheable、@CachePut 和 @CacheEvict,我們總結(jié)一下其作用和配置方法。
表 1. @Cacheable 作用和配置方法
@Cacheable 的作用 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進(jìn)行緩存
|
@Cacheable 主要的參數(shù) |
||
|
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個(gè) |
例如: @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 不同的是,它每次都會(huì)觸發(fā)真實(shí)方法的調(diào)用
|
@CachePut 主要的參數(shù) |
||
|
value |
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個(gè) |
例如: @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 配置文件中定義,必須指定至少一個(gè) |
例如: @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í)行的時(shí)候就清空緩存,缺省情況下,如果方法執(zhí)行拋出異常,則不會(huì)清空緩存 |
例如: @CachEvict(value=”testcache”,beforeInvocation=true) |
以上這篇基于spring 方法級(jí)緩存的多種實(shí)現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud消息總線Bus配置中心實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringCloud消息總線Bus配置中心實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java實(shí)現(xiàn)雪花算法的原理和實(shí)戰(zhàn)教程
這篇文章主要介紹了Java實(shí)現(xiàn)雪花算法的原理和實(shí)戰(zhàn)教程,本文通過語言表述和代碼的實(shí)現(xiàn)講解了該項(xiàng)算法,,需要的朋友可以參考下2021-06-06
SpringBoot同一接口多個(gè)實(shí)現(xiàn)類配置的實(shí)例詳解
這篇文章主要介紹了SpringBoot同一接口多個(gè)實(shí)現(xiàn)類配置的實(shí)例詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot實(shí)現(xiàn)嵌入式 Servlet容器
傳統(tǒng)的Spring MVC工程部署時(shí)需要將WAR文件放置在servlet容器的文檔目錄內(nèi),而Spring Boot工程使用嵌入式servlet容器省去了這一步驟,本文就來設(shè)置一下相關(guān)配置,感興趣的可以了解一下2023-12-12
使用Netty實(shí)現(xiàn)類似Dubbo的遠(yuǎn)程接口調(diào)用的實(shí)現(xiàn)方法
本文介紹了如何使用Netty框架實(shí)現(xiàn)類似Dubbo的遠(yuǎn)程接口調(diào)用,通過自定義編解碼器、通信協(xié)議和服務(wù)注冊中心等實(shí)現(xiàn)遠(yuǎn)程通信和服務(wù)治理。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
教你用Java Swing實(shí)現(xiàn)自助取款機(jī)系統(tǒng)
今天給大家?guī)淼氖顷P(guān)于JAVA的相關(guān)知識(shí),文章圍繞著如何用Java Swing實(shí)現(xiàn)自助取款機(jī)系統(tǒng)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06

