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

SpringBoot整合redis+lettuce的方法詳解

 更新時間:2023年08月24日 09:22:20   作者:fking86  
這篇文章主要介紹了SpringBoot整合redis+lettuce的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

Spring Boot提供了與Redis的集成框架,可以使用Lettuce作為Redis客戶端來進行整合。

版本依賴

jdk 17

SpringBoot 3.1.0

環(huán)境準備

依賴

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>RedisLettuceDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RedisLettuceDemo</name>
    <description>RedisLettuceDemo</description>
    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.32</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
    </dependencies>

配置

server:
  port: 8080  # 設(shè)置訪問端口
spring:
  redis:
    host: localhost
    port: 6379
    password: 123456
    database: 0
    ssl: false
    pool:
      maxIdle: 100
      minIdle: 0
      maxTotal: 100
      maxWaitMillis: 500
      testOnBorrow: false
      testOnReturn: true
      testWhileIdle: true

實例

LettuceClientConfig

//Redis服務(wù)器地址
    @Value("${spring.redis.host}")
    private String host;
    //Redis服務(wù)端口
    @Value("${spring.redis.port}")
    private Integer port;
    //Redis密碼
    @Value("${spring.redis.password}")
    private String password;
    //是否需要SSL
    @Value("${spring.redis.ssl}")
    private Boolean ssl;
    //Redis默認庫,一共0~15
    @Value("${spring.redis.database}")
    private Integer database;
    //Lettuce連接配置(Redis單機版實例)
    @Bean(name = "redisClient")
    public RedisClient redisClient() {
        RedisURI uri = RedisURI.Builder.redis(this.host, this.port)
                .withDatabase(this.database)
                .build();
        return RedisClient.create(uri);
    }

LettucePoolConfig

@Resource
    RedisClient redisClient;
    //設(shè)置可分配的最大Redis實例數(shù)量
    @Value("${spring.redis.pool.maxTotal}")
    private Integer maxTotal;
    //設(shè)置最多空閑的Redis實例數(shù)量
    @Value("${spring.redis.pool.maxIdle}")
    private Integer maxIdle;
    //歸還Redis實例時,檢查有消息,如果失敗,則銷毀實例
    @Value("${spring.redis.pool.testOnReturn}")
    private Boolean testOnReturn;
    //當Redis實例處于空閑壯體啊時檢查有效性,默認flase
    @Value("${spring.redis.pool.testWhileIdle}")
    private Boolean testWhileIdle;
    //Apache-Common-Pool是一個對象池,用于緩存Redis連接,
    //因為Letture本身基于Netty的異步驅(qū)動,但基于Servlet模型的同步訪問時,連接池是必要的
    //連接池可以很好的復(fù)用連接,減少重復(fù)的IO消耗與RedisURI創(chuàng)建實例的性能消耗
    @Getter
    GenericObjectPool<StatefulRedisConnection<String, String>> redisConnectionPool;
    //Servlet初始化時先初始化Lettuce連接池
    @PostConstruct
    private void init() {
        GenericObjectPoolConfig<StatefulRedisConnection<String, String>> redisPoolConfig
                = new GenericObjectPoolConfig<>();
        redisPoolConfig.setMaxIdle(this.maxIdle);
        redisPoolConfig.setMinIdle(0);
        redisPoolConfig.setMaxTotal(this.maxTotal);
        redisPoolConfig.setTestOnReturn(this.testOnReturn);
        redisPoolConfig.setTestWhileIdle(this.testWhileIdle);
        redisPoolConfig.setMaxWaitMillis(1000);
        this.redisConnectionPool =
                ConnectionPoolSupport.createGenericObjectPool(() -> redisClient.connect(), redisPoolConfig);
    }
    //Servlet銷毀時先銷毀Lettuce連接池
    @PreDestroy
    private void destroy() {
        redisConnectionPool.close();
        redisClient.shutdown();
    }

LettuceUtil

@Autowired
    LettucePoolConfig lettucePoolConfig;
    //編寫executeSync方法,在方法中,獲取Redis連接,利用Callback操作Redis,最后釋放連接,并返回結(jié)果
    //這里使用的同步的方式執(zhí)行cmd指令
    public <T> T executeSync(SyncCommandCallback<T> callback) {
        //這里利用try的語法糖,執(zhí)行完,自動給釋放連接
        try (StatefulRedisConnection<String, String> connection = lettucePoolConfig.getRedisConnectionPool().borrowObject()) {
            //開啟自動提交,如果false,命令會被緩沖,調(diào)用flushCommand()方法發(fā)出
            connection.setAutoFlushCommands(true);
            //設(shè)置為同步模式
            RedisCommands<String, String> commands = connection.sync();
            //執(zhí)行傳入的實現(xiàn)類
            return callback.doInConnection(commands);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
    //分裝一個set方法
    public String set(final String key, final String val) {
        return executeSync(commands -> commands.set(key, val));
    }
    //分裝一個get方法
    public String get(final String key) {
        return executeSync(commands -> commands.get(key));
    }

SyncCommandCallback

    //抽象方法,為了簡化代碼,便于傳入回調(diào)函數(shù)
    T doInConnection(RedisCommands<String, String> commands);

LettuceController

@Autowired
    LettuceUtil lettuceUtil;
    /**
     * 使用Lettuce工具類,調(diào)用Redis的Set指令
     * http://127.0.0.1:8080/lettuce/set?key=name&val=ipipman
     *
     * @param key
     * @param val
     * @return
     */
    @GetMapping("/set")
    public Object setItem(@RequestParam(name = "key", required = true) String key,
                          @RequestParam(name = "val", required = true) String val) {
        return lettuceUtil.set(key, val);
    }
    /**
     * 使用Lettuce工具類,調(diào)用Redis的Get指令
     * http://127.0.0.1:8080/lettuce/get?key=name
     *
     * @param key
     * @return
     */
    @GetMapping("/get")
    public Object getItem(@RequestParam(name = "key", required = true) String key) {
        return lettuceUtil.get(key);
    }

到此這篇關(guān)于SpringBoot整合redis+lettuce的方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合redis+lettuce內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java工具類DateUtils實例詳解

    Java工具類DateUtils實例詳解

    這篇文章主要為大家詳細介紹了Java工具類DateUtils實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • JavaSE中Lambda表達式的使用與變量捕獲

    JavaSE中Lambda表達式的使用與變量捕獲

    這篇文章主要介紹了JavaSE中Lambda表達式的使用與變量捕獲,Lambda表達式允許你通過表達式來代替功能接口, 就和方法一樣,它提供了一個正常的參數(shù)列表和一個使用這些參數(shù)的主體,下面我們來詳細看看,需要的朋友可以參考下
    2023-10-10
  • Java設(shè)計模式之享元模式實例詳解

    Java設(shè)計模式之享元模式實例詳解

    這篇文章主要介紹了Java設(shè)計模式之享元模式,結(jié)合實例形式詳細分析了享元模式的概念、功能、定義及使用方法,需要的朋友可以參考下
    2018-04-04
  • Springboot工具類StringUtils使用教程

    Springboot工具類StringUtils使用教程

    這篇文章主要介紹了Springboot內(nèi)置的工具類之StringUtils的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-12-12
  • java 數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序

    java 數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • Java8新特性之泛型的目標類型推斷_動力節(jié)點Java學院整理

    Java8新特性之泛型的目標類型推斷_動力節(jié)點Java學院整理

    泛型是Java SE 1.5的新特性,泛型的本質(zhì)是參數(shù)化類型,也就是說所操作的數(shù)據(jù)類型被指定為一個參數(shù)。下面通過本文給分享Java8新特性之泛型的目標類型推斷,感興趣的朋友參考下吧
    2017-06-06
  • java中的?HashMap?的加載因子是0.75原理探討

    java中的?HashMap?的加載因子是0.75原理探討

    在Java中,HashMap是一種常用的數(shù)據(jù)結(jié)構(gòu),用于存儲鍵值對,它的設(shè)計目標是提供高效的插入、查找和刪除操作,在HashMap的實現(xiàn)中,加載因子(Load?Factor)是一個重要的概念,本文將探討為什么Java中的HashMap的加載因子被設(shè)置為0.75
    2023-10-10
  • 一文搞懂Java項目中枚舉的定義與使用

    一文搞懂Java項目中枚舉的定義與使用

    枚舉就是用enum修飾是一種Java特殊的類,枚舉是class、底層是繼承了java.lang.Enum類的實體類。本文將詳解枚舉的定義與使用,需要的可以參考一下
    2022-06-06
  • 超詳細的Spring Boot入門筆記(總結(jié))

    超詳細的Spring Boot入門筆記(總結(jié))

    本篇文章主要介紹了超詳細的Spring Boot入門筆記(總結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 一文教你學會搭建SpringBoot分布式項目

    一文教你學會搭建SpringBoot分布式項目

    這篇文章主要為大家詳細介紹了搭建SpringBoot分布式項目的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-01-01

最新評論

遂川县| 甘南县| 乌拉特中旗| 衢州市| 衡山县| 安顺市| 攀枝花市| 绩溪县| 靖边县| 化州市| 沾化县| 栾城县| 江达县| 宿迁市| 库车县| 内江市| 嘉黎县| 沁阳市| 新竹县| 涟水县| 房产| 峡江县| 化州市| 弥勒县| 玛纳斯县| 楚雄市| 玛沁县| 台中市| 阜平县| 巴林左旗| 东方市| 年辖:市辖区| 乡城县| 连云港市| 辽阳市| 德惠市| 太原市| 墨江| 霸州市| 江川县| 衡南县|