集成Spring Redis緩存的實(shí)現(xiàn)
這里的緩存主要是用于 Service 層的,所以下面的配置,都是針對(duì) service 模塊的。
本文來(lái)自內(nèi)部分享,對(duì)特殊信息進(jìn)行了簡(jiǎn)單處理。
本文都是在以緩存來(lái)講 Redis 的使用,實(shí)際上 Redis 不僅僅用于緩存,本身還是 NoSQL 數(shù)據(jù)庫(kù),大家可以自己查找學(xué)習(xí) Redis 的常用場(chǎng)景。
一、添加依賴
<!--緩存--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!--redis--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.10.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
二、配置
增加spring-redis.xml
配置文件,內(nèi)容如下:
<?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:cache="http://www.springframework.org/schema/cache" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> <property name="testOnReturn" value="${redis.pool.testOnReturn}"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.master.ip}"/> <property name="port" value="${redis.master.port}"/> <property name="poolConfig" ref="jedisPoolConfig"/> </bean> <bean id="redisKeySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="redisValueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="keySerializer" ref="redisKeySerializer"/> <property name="hashKeySerializer" ref="redisKeySerializer"/> <property name="valueSerializer" ref="redisValueSerializer"/> <property name="hashValueSerializer" ref="redisValueSerializer"/> </bean> <!--在 redis.properties 配置緩存詳細(xì)信息--> <util:properties id="redisExpires" location="classpath*:META-INF/spring/redis.properties"/> <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg index="0" ref="redisTemplate"/> <!--默認(rèn)緩存 10 分鐘--> <property name="defaultExpiration" value="600"/> <property name="usePrefix" value="true"/> <property name="expires" ref="redisExpires"/> </bean> <!--啟用 cache 注解--> <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/> </beans>
在 src/main/resources/ 下面如果沒(méi)有META-INF/spring/
目錄就創(chuàng)建一個(gè),然后增加 redis.properties 配置,示例如下:
# 緩存名=有效時(shí)間 halfHour=1800 oneHour=3600 oneDay=86400 webSession=1800 user=1800
除了上面配置外,在系統(tǒng)的 application.properties 中還需要提供下面幾個(gè)配置:
# redis 連接配置 redis.master.ip=10.10.10.100 redis.master.port=6379 # redis 連接池配置 redis.pool.maxIdle=200 redis.pool.maxTotal=1024 redis.pool.maxWaitMillis=1000 redis.pool.testOnBorrow=true redis.pool.testOnReturn=true
三、通過(guò)注解方式使用緩存
示例中,redis.propreties 配置如下:
# 數(shù)據(jù)庫(kù)定義,緩存 30 天 databaseDef=2592000 # 數(shù)據(jù)庫(kù)元數(shù)據(jù),緩存 1 小時(shí) databaseMeta=3600
這個(gè)示例在數(shù)據(jù)庫(kù)服務(wù)上配置的,數(shù)據(jù)庫(kù)服務(wù)中,查詢次數(shù)遠(yuǎn)遠(yuǎn)大于新增、修改、刪除的次數(shù),非常適合使用緩存。
1. 緩存數(shù)據(jù) @Cacheable
@Override @Cacheable(value = "databaseDef", key = "'all'") public List<DatabaseDefinitionVo> selectAll() { return databaseDefinitionDao.selectAllVo(); }
特別注意:所有這些注解中,key 的值是 Spel 表達(dá)式,必須按照 Spel 要求來(lái)寫(xiě)。上面這個(gè)例子中,直接定義返回值的 key 是 all 字符串,需要加上單引號(hào)' 括起來(lái),下面還有其他用法。
在例子中,下面的方法也使用了這個(gè)注解:
@Override @Cacheable(value = "databaseDef", key = "#id.toString()") public DatabaseDefinition selectByPrimaryKey(Long id) { Assert.notNull(id, "數(shù)據(jù)庫(kù) ID 不能為空!"); DatabaseDefinition definition = databaseDefinitionDao.selectByPrimaryKey(id); Assert.notNull(definition, "數(shù)據(jù)庫(kù)定義不存在!"); return definition; }
在上面注解中,key
中的 #id
指的是參數(shù)中的 id,在 IDEA 中會(huì)有自動(dòng)提示。.toString()
是調(diào)用 id
的方法,在系統(tǒng)中規(guī)定了 key 必須是字符串類型,所以當(dāng)類型是 Long 的時(shí)候,需要轉(zhuǎn)換。
使用緩存的目的就是為了減少上面兩個(gè)方法調(diào)用時(shí)減少和數(shù)據(jù)庫(kù)的交互,減小數(shù)據(jù)庫(kù)的壓力,這是兩個(gè)主要的緩存數(shù)據(jù)的方法,下面的幾個(gè)操作都和上面這兩個(gè)方法有一定的關(guān)系。
重點(diǎn):這里以及下面幾個(gè)注解中,都指定了
value = "databaseDef"
,這里的意思是說(shuō),要使用前面配置中的 databaseDef 對(duì)應(yīng)的配置,也就是會(huì)緩存 30天。
2. 更新緩存 @CachePut
@Override @CachePut(value = "databaseDef", key = "#result.id.toString()") public DatabaseDefinition save(DatabaseDefinition definition, CurrentUser userModel) throws ServiceException { //代碼 return definition; }
更新緩存的方法需要注意的是返回值,在上面 save 方法中,有可能是新增,有可能是更新,不管是那個(gè)操作,當(dāng)操作完成后,上面注解會(huì)根據(jù) key 的值生成 key,然后將方法的返回值作為 value 保存到緩存中。
這里 key 的寫(xiě)法中 #result 指代返回值,.id 是返回值的屬性,.toString() 是調(diào)用 id 屬性的方法,在系統(tǒng)中規(guī)定了 key 必須是字符串類型,所以當(dāng)類型是 Long 的時(shí)候,需要轉(zhuǎn)換。
這個(gè)方法上加的緩存還有問(wèn)題,當(dāng)新增或者更新后,通過(guò) selectAll() 返回的值已經(jīng)發(fā)生了變化,但是這里沒(méi)有清除 all 的緩存值,會(huì)導(dǎo)致 selectAll() 出現(xiàn)臟數(shù)據(jù),下面會(huì)通過(guò) @Caching 注解改造這里。
3. 清除緩存 @CacheEvict
@Override @CacheEvict(value = "databaseDef", key = "#id.toString()") public void deleteByPrimaryKey(Long id) throws ServiceException { DatabaseDefinition definition = selectByPrimaryKey(id); if (definition.getLoadState().equals(DatabaseDefinition.LoadState.UP)) { throw new ServiceException("請(qǐng)先卸載數(shù)據(jù)庫(kù)!"); } databaseDefinitionDao.deleteByPrimaryKey(id); }
在上面新增或者修改的時(shí)候根據(jù) id 緩存或者更新了緩存數(shù)據(jù),這里當(dāng)刪除數(shù)據(jù)的時(shí)候,還需要清空對(duì)應(yīng)的緩存數(shù)據(jù)。
在上面注解中,key 中的 #id 指的是參數(shù)中的 id,在 IDEA 中會(huì)有自動(dòng)提示。
這個(gè)方法上加的緩存還有問(wèn)題,當(dāng)刪除后,通過(guò) selectAll() 返回的值已經(jīng)發(fā)生了變化,但是這里沒(méi)有清除 all 的緩存值,會(huì)導(dǎo)致 selectAll() 出現(xiàn)臟數(shù)據(jù),下面會(huì)通過(guò) @Caching 注解改造這里。
4. 組合使用 @Caching
上面兩個(gè)注解中,都提到了臟數(shù)據(jù),通過(guò) @Caching 注解可以解決這個(gè)問(wèn)題。
先修改第二個(gè)注解,來(lái)解決 save 時(shí)的臟數(shù)據(jù):
@Override @Caching(put = @CachePut(value = "databaseDef", key = "#result.id.toString()"), evict = @CacheEvict(value = "databaseDef", key = "'all'")) public DatabaseDefinition save(DatabaseDefinition definition, CurrentUser userModel) throws ServiceException { //其他代碼 return definition; }
前面說(shuō)明,新增或者修改的時(shí)候,all 緩存中的數(shù)據(jù)已經(jīng)不對(duì)了,因此這里在 put 的同時(shí),使用 evict 將 'all' 中的數(shù)據(jù)清除,這就保證了 selelctAll 下次調(diào)用時(shí),會(huì)重新從庫(kù)中讀取數(shù)據(jù)。
對(duì)上面的刪除方法,也進(jìn)行類似的修改:
@Override @Caching(evict = { @CacheEvict(value = "databaseDef", key = "#id.toString()"), @CacheEvict(value = "databaseDef", key = "'all'") }) public void deleteByPrimaryKey(Long id) throws ServiceException { DatabaseDefinition definition = selectByPrimaryKey(id); if (definition.getLoadState().equals(DatabaseDefinition.LoadState.UP)) { throw new ServiceException("請(qǐng)先卸載數(shù)據(jù)庫(kù)!"); } databaseDefinitionDao.deleteByPrimaryKey(id); }
注意這里的 evict 是個(gè)數(shù)組,里面配置了兩個(gè)清除緩存的配置。
5. 全局配置 @CacheConfig
在上面所有例子中,都指定了 value = "databaseDef",實(shí)際上可以通過(guò)在類上使用 @CacheConfig 注解配置當(dāng)前類中的 cacheNames 值,配置后,如果和類上的 value 一樣就不需要在每個(gè)注解單獨(dú)配置。只有不同時(shí)再去指定,方法上的 value 值優(yōu)先級(jí)更高。
@Service @CacheConfig(cacheNames = "databaseDef") public class DatabaseDefinitionServiceImpl implements DatabaseDefinitionService, DatabaseSqlExecuteService, ApplicationListener {
有了上面配置后,其他緩存名字相同的地方可以簡(jiǎn)化,例如刪除方法修改后如下:
@Override @Caching(evict = { @CacheEvict(key = "#id.toString()"), @CacheEvict(key = "'all'") }) public void deleteByPrimaryKey(Long id) throws ServiceException { DatabaseDefinition definition = selectByPrimaryKey(id); if (definition.getLoadState().equals(DatabaseDefinition.LoadState.UP)) { throw new ServiceException("請(qǐng)先卸載數(shù)據(jù)庫(kù)!"); } databaseDefinitionDao.deleteByPrimaryKey(id); }
其他例子
除了上面針對(duì) databaseDef 的緩存外,還有 databaseMeta 的配置:
@Override @Cacheable(value = "databaseMeta", key = "#databaseId.toString()") public List<TableVo> selectTablesByDatabaseId(Long databaseId) throws Exception { //代碼 } @Override @Cacheable(value = "databaseMeta", key = "#databaseId + '_' + #tableName") public TableVo selectTableByDatabaseIdAndTableName(Long databaseId, String tableName) throws Exception { //代碼 }
這兩個(gè)方法是獲取數(shù)據(jù)庫(kù)元數(shù)據(jù)的,只有修改數(shù)據(jù)庫(kù)表的時(shí)候才會(huì)變化,因此不存在清除緩存的情況,但是萬(wàn)一修改表后,想要新的元數(shù)據(jù),該怎么辦?
因此增加了一個(gè)空的方法來(lái)清空數(shù)據(jù),方法如下:
@Override @CacheEvict(value = "databaseMeta", allEntries = true) public void cleanTablesCache() { }
這里指定了 databaseMeta,通過(guò) allEntries = true 清空所有 key 的緩存。通過(guò)這個(gè)方法可以保證在有需要的時(shí)候清空所有元數(shù)據(jù)的緩存。
實(shí)際上如果想要更精確的清除,可以傳入要清除的 databaseId 和 tableName 來(lái)更精確的清除。
增加緩存后的效果
調(diào)用 selectTablesByDatabaseId 多次時(shí),輸出的日志如下:
INFO c.n.d.u.DynamicDataSource - 當(dāng)前數(shù)據(jù)源:默認(rèn)數(shù)據(jù)源
DEBUG c.n.d.DataScopeContextProviderFilter - 服務(wù)調(diào)用返回前清除數(shù)據(jù)權(quán)限信息
INFO c.n.d.u.DynamicDataSource - 當(dāng)前數(shù)據(jù)源:默認(rèn)數(shù)據(jù)源
DEBUG c.n.d.d.D.selectByPrimaryKey - ==> Preparing: SELECT xxx (隱藏完整 SQL)
DEBUG c.n.d.d.D.selectByPrimaryKey - ==> Parameters: 1(Long)
DEBUG c.n.d.d.D.selectByPrimaryKey - <== Total: 1
INFO c.n.d.u.DynamicDataSource - 當(dāng)前數(shù)據(jù)源:10.10.10.130/datareporting
DEBUG c.n.d.DataScopeContextProviderFilter - 服務(wù)調(diào)用返回前清除數(shù)據(jù)權(quán)限信息
INFO c.n.d.u.DynamicDataSource - 當(dāng)前數(shù)據(jù)源:默認(rèn)數(shù)據(jù)源
DEBUG c.n.d.DataScopeContextProviderFilter - 服務(wù)調(diào)用返回前清除數(shù)據(jù)權(quán)限信息
INFO c.n.d.u.DynamicDataSource - 當(dāng)前數(shù)據(jù)源:默認(rèn)數(shù)據(jù)源
DEBUG c.n.d.DataScopeContextProviderFilter - 服務(wù)調(diào)用返回前清除數(shù)據(jù)權(quán)限信息
從日志可以看出來(lái),只有第一次進(jìn)行了數(shù)據(jù)庫(kù)查詢,后續(xù)通過(guò)日志看不到數(shù)據(jù)庫(kù)操作。
調(diào)用清空緩存后,會(huì)再次查詢數(shù)據(jù)庫(kù)。
初次調(diào)用時(shí),WEB請(qǐng)求花了 700多ms,后面再次調(diào)用時(shí),平均不到 30 ms,這就是緩存最明顯的作用。
連接到 redis 服務(wù)后,查看所有 key,結(jié)果如下:
redis@redissvr:~$ redis-cli
127.0.0.1:6379> keys *
1) "databaseMeta:1"
2) "databaseDef:all"
127.0.0.1:6379>
緩存中的數(shù)據(jù)都有 value 前綴,上面緩存了 all 和 id 為 1 的數(shù)據(jù)。
緩存注解是一種最簡(jiǎn)單的緩存方式,但是需要配合 value 屬性的配置來(lái)使用,許多時(shí)候我們可能需要更精確的控制緩存,此時(shí)可以使用 RedisTemplate 來(lái)控制。
四、通過(guò) RedisTemplate 使用緩存
有關(guān)這部分的詳細(xì)用法可以從網(wǎng)上搜索相關(guān)內(nèi)容進(jìn)行學(xué)習(xí),這里列舉一個(gè)簡(jiǎn)單的例子。
針對(duì)前面的 selectAll 我們換一種方式進(jìn)行緩存。
首先注入下面的接口:
@Resource(name = "redisTemplate") private ValueOperations<String, List> valueOper;
修改 selectAll 方法如下:
@Override //@Cacheable(key = "'all'") public List<DatabaseDefinitionVo> selectAll() { List<DatabaseDefinitionVo> vos = valueOper.get("databaseDef:all"); if(vos != null){ return vos; } vos = databaseDefinitionDao.selectAllVo(); //緩存 1 小時(shí) valueOper.set("databaseDef:all", vos, 1, TimeUnit.HOURS); return vos; }
首先通過(guò)valueOper.get("databaseDef:all")
嘗試獲取緩存信息,如果存在就直接返回。
如果不存在,就查詢數(shù)據(jù)庫(kù),然后將查詢結(jié)果通過(guò) set 進(jìn)行緩存。
特別注意: 上面的 key,寫(xiě)的是 "databaseDef:all",也就是前綴需要自己加上,如果直接寫(xiě)成 all,在 Redis 中的 key 就是 all,不會(huì)自動(dòng)增加前綴。
如果沒(méi)有前綴,那么當(dāng)不同系統(tǒng)都使用 all 時(shí),數(shù)據(jù)就會(huì)混亂!
五、Redis 服務(wù)器配置注意事項(xiàng)
內(nèi)網(wǎng)使用的服務(wù)器,特殊配置如下:
# By default protected mode is enabled. You should disable it only if # you are sure you want clients from other hosts to connect to Redis # even if no authentication is configured, nor a specific set of interfaces # are explicitly listed using the "bind" directive. # protected-mode yes protected-mode no
關(guān)閉了保護(hù)模式。
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # bind 127.0.0.1
注釋了綁定的 IP,這樣可以讓所有電腦訪問(wèn) Redis。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Java實(shí)現(xiàn)選擇排序算法的實(shí)例教程
這篇文章主要介紹了Java實(shí)現(xiàn)選擇排序算法的實(shí)例教程,選擇排序的時(shí)間復(fù)雜度為О(n²),需要的朋友可以參考下2016-05-05springboot FeignClient注解及參數(shù)
這篇文章主要介紹了springboot FeignClient注解及參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Quarkus云原生開(kāi)篇java框架簡(jiǎn)介
Quarkus?是小紅帽開(kāi)源的專門針對(duì)云容器環(huán)境優(yōu)化的云原生java框架,博主接下來(lái)的項(xiàng)目估計(jì)都會(huì)使用這個(gè)框架來(lái)開(kāi)發(fā),相關(guān)的問(wèn)題都會(huì)記錄在這個(gè)系列,本文是個(gè)開(kāi)篇2022-02-02java反射實(shí)現(xiàn)javabean轉(zhuǎn)json實(shí)例代碼
基于java反射機(jī)制實(shí)現(xiàn)javabean轉(zhuǎn)json字符串實(shí)例,大家參考使用吧2013-12-12java網(wǎng)上圖書(shū)商城(4)購(gòu)物車模塊1
這篇文章主要為大家詳細(xì)介紹了java網(wǎng)上圖書(shū)商城,購(gòu)物車模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12源碼解讀Spring-Integration執(zhí)行過(guò)程
Spring-Integration基于Spring,在應(yīng)用程序中啟用了輕量級(jí)消息傳遞,并支持通過(guò)聲明式適配器與外部系統(tǒng)集成,今天主要是看個(gè)簡(jiǎn)單的hello word進(jìn)來(lái)分析下整個(gè)執(zhí)行過(guò)程,感興趣的朋友一起看看吧2021-06-06