Redis設(shè)置database不生效的解決方案
前言
事情是這樣的 今天在拉取了同事的代碼做redis緩存設(shè)置的時候,發(fā)現(xiàn)即使已經(jīng)設(shè)置了database, 但是存數(shù)據(jù)的時候還是用的默認0數(shù)據(jù)庫。這引起了我的好奇,遂開始琢磨是什么情況造成的這種現(xiàn)象。
配置
上述僅為測試代碼問題,為了便于維護可以這么寫,
spring: redis: host: ${REDIS_HOST:localhost} port: ${REDIS_PORT:6379} password: ${REDIS_PASSWORD:} database: ${REDIS_DATABASE:0}
加載類
然后通過RedisConfiguration 加載
@ConfigurationProperties("spring.redis") public class RedisConfiguration { private String host; private int port; private String password; private int database; // getters and setters... }
問題
上網(wǎng)找了一系列的文章都沒解決,后來仔細觀察研究發(fā)現(xiàn)是database多了個空格,正確的該是這樣,沒想到一個空格浪費了這么多時間
信心滿滿的以為這就萬事大吉了,結(jié)果一運行發(fā)現(xiàn)依然不可以,后又開始檢查,最后發(fā)現(xiàn)是少了幾個依賴
發(fā)現(xiàn)沒有引入commons-pool2依賴,加上了依賴之后再運行發(fā)現(xiàn)已經(jīng)切換了也可以引入lettuce-core 依賴
<!-- lettuce-core --> <dependency> <groupId>io.lettuce.core</groupId> <artifactId>lettuce-core</artifactId> </dependency>
commons-pool 對象池
引入Commons Pool對象池,用于緩存Redis連接的原因是因為Lettuce本身是基于Netty的異步驅(qū)動,在異步訪問時并不需要創(chuàng)建連接池,但基于Servlet模型的同步訪問時,連接池是有必要的。目的是為了復(fù)用對象,以減少創(chuàng)建對象的開銷,所以一定記得要加這個依賴。
/** * Creates an instance that can be served by the pool and wrap it in a * {@link PooledObject} to be managed by the pool. * * @return a {@code PooledObject} wrapping an instance that can be served by the pool * * @throws Exception if there is a problem creating a new instance, * this will be propagated to the code requesting an object. */ PooledObject makeObject()throws Exception; /** * Destroys an instance no longer needed by the pool. * <p>* It is important for implementations of this method to be aware that there * is no guarantee about what state <code>obj</code>will be in and the * implementation should be prepared to handle unexpected errors. * </p> * <p>* Also, an implementation must take in to consideration that instances lost * to the garbage collector may never be destroyed. * </p>* * @param p a {@code PooledObject} wrapping the instance to be destroyed * * @throws Exception should be avoided as it may be swallowed by * the pool implementation. * * @see #validateObject * @see ObjectPool#invalidateObject */ void destroyObject(PooledObject p)throws Exception; /** * Ensures that the instance is safe to be returned by the pool. * * @param p a {@code PooledObject} wrapping the instance to be validated * * @return <code>false</code> if <code>obj</code>is not valid and should * be dropped from the pool, <code>true</code>otherwise. */ boolean validateObject(PooledObject p); /** * Reinitializes an instance to be returned by the pool. * * @param p a {@code PooledObject} wrapping the instance to be activated * * @throws Exception if there is a problem activating <code>obj</code>, * this exception may be swallowed by the pool. * * @see #destroyObject */ void activateObject(PooledObject p)throws Exception; /** * Uninitializes an instance to be returned to the idle object pool. * * @param p a {@code PooledObject} wrapping the instance to be passivated * * @throws Exception if there is a problem passivating <code>obj</code>, * this exception may be swallowed by the pool. * * @see #destroyObject */ void passivateObject(PooledObject p)throws Exception;
注意:
Jedis 和Lettuce 是Java 操作Redis 的客戶端。
在Spring Boot 1.x 版本默認使用的是Jedis ,而在Spring Boot 2.x 版本默認使用的就是Lettuce。所以如果你用的是1.x版本的話 需要把 RedisConnectionFactory factory 替換為LettuceConnectionFactory lettuceConnectionFactory同時在依賴中排除lettuce的jar 改為用jedis
對比
commons-pool2
和 lettuce-core
都是在處理 Redis 連接池方面常用的依賴,但它們有不同的設(shè)計和適用場景,因此在選擇使用哪個依賴時需要根據(jù)具體情況進行權(quán)衡。
commons-pool2
是 Apache Commons 項目中的一個組件,用于實現(xiàn)通用的對象池。它不僅僅適用于 Redis 連接池,還可以用于其他對象的池化管理。 commons-pool2
的設(shè)計比較通用,允許你管理任意類型的對象池,因此可以更靈活地適應(yīng)不同的場景。如果你的項目需要管理多個類型的對象池,或者你希望在 Redis 之外使用對象池功能,那么選擇 commons-pool2
是一個不錯的選擇。
另一方面, lettuce-core
是一個專門為 Redis 設(shè)計的客戶端庫。它提供了豐富的功能和性能優(yōu)化,專注于提供高效的 Redis 連接和操作。 lettuce-core
自帶了連接池的功能,你可以使用它內(nèi)置的連接池來管理 Redis 連接,無需額外的依賴。
所以,選擇使用 commons-pool2
還是 lettuce-core
取決于你的項目需求:
- 如果你只需要管理 Redis 連接池,而不需要通用的對象池功能,那么使用
lettuce-core
內(nèi)置的連接池可能更為方便和簡潔。 - 如果你的項目需要管理多個類型的對象池,或者需要在其他場景中使用對象池,那么
commons-pool2
提供的通用對象池功能可能更適合
到此這篇關(guān)于Redis設(shè)置database不生效的解決方案的文章就介紹到這了,更多相關(guān)Redis設(shè)置database不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝的方法
這篇文章主要介紹了基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪功能(示例代碼)
基于 spring aop 常規(guī)應(yīng)用場景多是用于日志記錄以及實現(xiàn) redis 分布式鎖,在 github 中也有項目是把它拿來當作緩存的異常捕捉,這篇文章主要介紹了基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪,需要的朋友可以參考下2022-08-08