欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Redis設(shè)置database不生效的解決方案

 更新時間:2023年08月16日 09:17:09   作者:家有嬌妻張兔兔  
最近在做redis緩存設(shè)置的時候,發(fā)現(xiàn)即使已經(jīng)設(shè)置了database, 但是存數(shù)據(jù)的時候還是用的默認0數(shù)據(jù)庫,所以本文就給大家介紹了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內(nèi)存滿了的幾種原因和最佳解決方案

    Redis內(nèi)存滿了的幾種原因和最佳解決方案

    Redis是一款高性能的內(nèi)存數(shù)據(jù)庫,被廣泛應(yīng)用于緩存、消息隊列、計數(shù)器等場景,然而,由于Redis是基于內(nèi)存的數(shù)據(jù)庫,當數(shù)據(jù)量過大或者配置不合理時,就有可能導(dǎo)致Redis的內(nèi)存滿,本文將介紹Redis內(nèi)存滿的幾種原因,并提供相應(yīng)的解決方案,需要的朋友可以參考下
    2023-11-11
  • Redis高性能的原因及說明

    Redis高性能的原因及說明

    這篇文章主要介紹了Redis高性能的原因及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝的方法

    基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝的方法

    這篇文章主要介紹了基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Redis持久化與主從復(fù)制的實踐

    Redis持久化與主從復(fù)制的實踐

    這篇文章主要介紹了Redis持久化與主從復(fù)制的實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • 幾分鐘教你掌握Redis簡單動態(tài)字符串SDS

    幾分鐘教你掌握Redis簡單動態(tài)字符串SDS

    這篇文章主要為大家介紹了幾分鐘教你掌握Redis簡單動態(tài)字符串SDS方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 詳解SSH框架和Redis的整合

    詳解SSH框架和Redis的整合

    本篇文章主要介紹了SSH框架和Redis的整合,詳細的介紹了Struts+Spring+Hibernate和Redis整合,有興趣的可以了解一下。
    2017-03-03
  • Redis哨兵改集群的方法實現(xiàn)

    Redis哨兵改集群的方法實現(xiàn)

    Redis作為一個開源的鍵值存儲系統(tǒng),廣泛應(yīng)用于各種場景,如緩存和消息隊列,為了提高可用性和擴展性,可以將Redis哨兵架構(gòu)改為集群架構(gòu),本文就來介紹一下,感興趣的可以了解一下
    2024-09-09
  • Redis高可用梳理詳解

    Redis高可用梳理詳解

    高可用的本質(zhì)是有備份,在出現(xiàn)故障的時候,有backup可以提供服務(wù),本文詳細介紹了Redis的高可用,感興趣的同學可以參考閱讀
    2023-05-05
  • SpringBoot 開啟Redis緩存及使用方法

    SpringBoot 開啟Redis緩存及使用方法

    用redis做緩存,是因為redis有著很優(yōu)秀的讀寫能力,在集群下可以保證數(shù)據(jù)的高可用,那么今天通過本文給大家講解下SpringBoot使用Redis的緩存的方法,感興趣的朋友一起看看吧
    2021-08-08
  • 基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪功能(示例代碼)

    基于?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

最新評論