一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 基于spring 方法級緩存的多種實現

基于spring 方法級緩存的多種實現

2021-01-06 11:04劉竹青 Java教程

下面小編就為大家帶來一篇基于spring 方法級緩存的多種實現。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

方案實施

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产麻豆视频 | 日韩精品视频在线播放 | 国产一区二区三区高清 | 日日骑夜夜骑 | 波多野结衣在线观看中文字幕 | re99热| 亚洲久草| 亚洲美女人黄网成人女 | 嫩草香味 | 国产90后美女露脸在线观看 | 亚洲欧美日韩成人一区在线 | 精品一区二区三区免费观看 | 国产爱啪啪 | 91精品国产在线 | 亚洲高清在线精品一区 | 人人最怕九月羊 | 国产一级真人毛爱做毛片 | 91麻豆精品| free嫩白的12sex性自由 | 日本三级欧美三级人妇英文 | 免费特黄一级欧美大片 | 狠狠色综合久久婷婷 | 男人的天堂久久 | 美女被绑着吸下部的故事 | 女暴露狂校园裸露小说 | 久久久免费观看 | 久九九精品免费视频 | 国产 日韩 欧美 综合 | 欧美不卡一区二区三区 | 国产成人一级 | 娇妻与公陈峰姚瑶小说在线阅读 | 久久综合给合久久狠狠狠… | 国产在线精品99一卡2卡 | 亚洲成人99| 91成人免费视频 | 肥奶丰熟肥妇 | 久久婷婷五月综合色精品首页 | 午夜性爽视频男人的天堂在线 | 美女班主任让我爽了一夜视频 | 性导航h| 欧美涩区 |