最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2023年08月16日 09:17:09   作者:家有嬌妻張兔兔  
最近在做redis緩存設(shè)置的時(shí)候,發(fā)現(xiàn)即使已經(jīng)設(shè)置了database, 但是存數(shù)據(jù)的時(shí)候還是用的默認(rèn)0數(shù)據(jù)庫,所以本文就給大家介紹了Redis設(shè)置database不生效的解決方案,需要的朋友可以參考下

在這里插入圖片描述

前言

事情是這樣的 今天在拉取了同事的代碼做redis緩存設(shè)置的時(shí)候,發(fā)現(xiàn)即使已經(jīng)設(shè)置了database, 但是存數(shù)據(jù)的時(shí)候還是用的默認(rèn)0數(shù)據(jù)庫。這引起了我的好奇,遂開始琢磨是什么情況造成的這種現(xiàn)象。

配置

在這里插入圖片描述

上述僅為測試代碼問題,為了便于維護(hù)可以這么寫,

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)找了一系列的文章都沒解決,后來仔細(xì)觀察研究發(fā)現(xiàn)是database多了個(gè)空格,正確的該是這樣,沒想到一個(gè)空格浪費(fèi)了這么多時(shí)間

在這里插入圖片描述

信心滿滿的以為這就萬事大吉了,結(jié)果一運(yùn)行發(fā)現(xiàn)依然不可以,后又開始檢查,最后發(fā)現(xiàn)是少了幾個(gè)依賴

在這里插入圖片描述

發(fā)現(xiàn)沒有引入commons-pool2依賴,加上了依賴之后再運(yùn)行發(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連接的原因是因?yàn)長ettuce本身是基于Netty的異步驅(qū)動,在異步訪問時(shí)并不需要創(chuàng)建連接池,但基于Servlet模型的同步訪問時(shí),連接池是有必要的。目的是為了復(fù)用對象,以減少創(chuàng)建對象的開銷,所以一定記得要加這個(gè)依賴。

/**
* 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 版本默認(rèn)使用的是Jedis ,而在Spring Boot 2.x 版本默認(rèn)使用的就是Lettuce。所以如果你用的是1.x版本的話 需要把 RedisConnectionFactory factory 替換為LettuceConnectionFactory lettuceConnectionFactory同時(shí)在依賴中排除lettuce的jar 改為用jedis

在這里插入圖片描述

對比

commons-pool2 lettuce-core 都是在處理 Redis 連接池方面常用的依賴,但它們有不同的設(shè)計(jì)和適用場景,因此在選擇使用哪個(gè)依賴時(shí)需要根據(jù)具體情況進(jìn)行權(quán)衡。

commons-pool2 是 Apache Commons 項(xiàng)目中的一個(gè)組件,用于實(shí)現(xiàn)通用的對象池。它不僅僅適用于 Redis 連接池,還可以用于其他對象的池化管理。 commons-pool2 的設(shè)計(jì)比較通用,允許你管理任意類型的對象池,因此可以更靈活地適應(yīng)不同的場景。如果你的項(xiàng)目需要管理多個(gè)類型的對象池,或者你希望在 Redis 之外使用對象池功能,那么選擇 commons-pool2 是一個(gè)不錯的選擇。

另一方面, lettuce-core 是一個(gè)專門為 Redis 設(shè)計(jì)的客戶端庫。它提供了豐富的功能和性能優(yōu)化,專注于提供高效的 Redis 連接和操作。 lettuce-core 自帶了連接池的功能,你可以使用它內(nèi)置的連接池來管理 Redis 連接,無需額外的依賴。

所以,選擇使用 commons-pool2 還是 lettuce-core 取決于你的項(xiàng)目需求:

  • 如果你只需要管理 Redis 連接池,而不需要通用的對象池功能,那么使用 lettuce-core 內(nèi)置的連接池可能更為方便和簡潔。
  • 如果你的項(xiàng)目需要管理多個(gè)類型的對象池,或者需要在其他場景中使用對象池,那么 commons-pool2 提供的通用對象池功能可能更適合

到此這篇關(guān)于Redis設(shè)置database不生效的解決方案的文章就介紹到這了,更多相關(guān)Redis設(shè)置database不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文掌握Redis的三種集群方案(小結(jié))

    一文掌握Redis的三種集群方案(小結(jié))

    這篇文章主要介紹了一文掌握Redis的三種集群方案(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Redis密碼設(shè)置與訪問限制實(shí)現(xiàn)方法

    Redis密碼設(shè)置與訪問限制實(shí)現(xiàn)方法

    這篇文章主要介紹了Redis密碼設(shè)置與訪問限制實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • php結(jié)合redis實(shí)現(xiàn)高并發(fā)下的搶購、秒殺功能的實(shí)例

    php結(jié)合redis實(shí)現(xiàn)高并發(fā)下的搶購、秒殺功能的實(shí)例

    下面小編就為大家?guī)硪黄猵hp結(jié)合redis實(shí)現(xiàn)高并發(fā)下的搶購、秒殺功能的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • Redis的5種數(shù)據(jù)類型與常用命令講解

    Redis的5種數(shù)據(jù)類型與常用命令講解

    今天小編就為大家分享一篇關(guān)于Redis的5種數(shù)據(jù)類型與常用命令講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Redis與RabbitMQ的區(qū)別對比和結(jié)合應(yīng)用

    Redis與RabbitMQ的區(qū)別對比和結(jié)合應(yīng)用

    RabbitMQ和Redis是兩種流行的消息隊(duì)列(Message Queue)和緩存系統(tǒng),在應(yīng)用程序開發(fā)中起著不同的角色和功能,Redis憑借內(nèi)存存儲和豐富數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)高速緩存和分布式鎖,RabbitMQ通過消息隊(duì)列實(shí)現(xiàn)系統(tǒng)解耦和異步處理,二者結(jié)合可應(yīng)對電商秒殺等高并發(fā)場景
    2025-10-10
  • redis設(shè)置密碼并修改查看的幾種實(shí)現(xiàn)過程

    redis設(shè)置密碼并修改查看的幾種實(shí)現(xiàn)過程

    這篇文章主要介紹了redis設(shè)置密碼并修改查看的幾種實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-06-06
  • redisson鎖tryLock的正確使用方式

    redisson鎖tryLock的正確使用方式

    這篇文章主要介紹了redisson鎖tryLock的正確使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • k8s部署redis集群搭建過程示例詳解

    k8s部署redis集群搭建過程示例詳解

    這篇文章主要為大家介紹了k8s部署redis集群搭建過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Redis?緩存淘汰策略和事務(wù)實(shí)現(xiàn)樂觀鎖詳情

    Redis?緩存淘汰策略和事務(wù)實(shí)現(xiàn)樂觀鎖詳情

    這篇文章主要介紹了Redis緩存淘汰策略和事務(wù)實(shí)現(xiàn)樂觀鎖詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • 詳解Redis緩存預(yù)熱的實(shí)現(xiàn)方法

    詳解Redis緩存預(yù)熱的實(shí)現(xiàn)方法

    緩存預(yù)熱是一種在程序啟動或緩存失效之后,主動將熱點(diǎn)數(shù)據(jù)加載到緩存中的策略,本文將給大家分享一下如何實(shí)現(xiàn)Redis的緩存預(yù)熱,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2023-10-10

最新評論

中超| 桐梓县| 光泽县| 津市市| 高碑店市| 嘉鱼县| 论坛| 胶州市| 嘉峪关市| 华容县| 临武县| 宣恩县| 伊通| 克山县| 乐平市| 岳西县| 靖宇县| 陕西省| 应用必备| 呼伦贝尔市| 古田县| 阿尔山市| 上高县| 绥中县| 麻阳| 凯里市| 城口县| 嘉禾县| 阳曲县| 阳新县| 焉耆| 邹城市| 灌南县| 新宾| 阳新县| 博野县| 海丰县| 嘉义市| 金溪县| 潢川县| 正安县|