首頁 Java java教程 SpringBoot中如何整合Lettuce redis

SpringBoot中如何整合Lettuce redis

May 15, 2023 pm 08:52 PM
springboot

1、新增依賴

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>
登入後複製

2、新增redis配置

spring:
 redis:
   host: ****
   password:****
   port: 6379
 # 连接超时时间(毫秒)
   timeout: 1000
 # Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
   database: 0
 # 连接池配置
   lettuce:
    pool:
 # 连接池最大连接数(使用负值表示没有限制) 默认 8
     max-active: 8
 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
     max-wait: -1
 # 连接池中的最大空闲连接 默认 8
     max-idle: 8
 # 连接池中的最小空闲连接 默认 0
     min-idle: 0
登入後複製

3、實作邏輯

@Autowired
  private StringRedisTemplate stringRedisTemplate;
  @Override
  public String testRedis(){
    ExecutorService executorService = Executors.newFixedThreadPool(1000);
    IntStream.range(0, 1000).forEach(i -> executorService.execute(() -> stringRedisTemplate.opsForValue().increment("lcl",1)));
    System.out.println("lcl1=============" + stringRedisTemplate.opsForValue().get("lcl"));
    stringRedisTemplate.opsForValue().set("lcl1","val1");
    String val1 = stringRedisTemplate.opsForValue().get("lcl1");
    System.out.println("lcl1=============" + val1);
    String key = "redis:test:demo1";
    User user = new User();
    user.setId(100L);
    user.setUsername("u2");
    user.setPassword("p2");
    stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(user));
    String valUser = stringRedisTemplate.opsForValue().get(key);
    System.out.println("redis:test:demo1=============" + valUser);
    User getUser = JSON.parseObject(valUser, User.class);
    System.out.println("redis:test:demo1=============" + getUser.getUsername()+ "========" + getUser.getPassword());
    return null;
  }
登入後複製

測試結果:

SpringBoot中如何整合Lettuce redis

SpringBoot中如何整合Lettuce redis

#由於redis有String、list、set、zset、hash、geo等類型,因此使用時不只使用opsForValue()方法,具體的對應方法如下:

  • opsForValue: 對應String(字串)

  • opsForZSet: 對應ZSet(有序集合)

  • ##opsForHash: 對應Hash(哈希)

  • opsForList: 對應List(列表)

  • opsForSet: 對應Set(集合)

  • #opsForGeo: 對應GEO(地理位置)

以上是SpringBoot中如何整合Lettuce redis的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Springboot怎麼整合Jasypt實現設定檔加密 Springboot怎麼整合Jasypt實現設定檔加密 Jun 01, 2023 am 08:55 AM

Springboot怎麼整合Jasypt實現設定檔加密

怎麼在SpringBoot中使用Redis實現分散式鎖 怎麼在SpringBoot中使用Redis實現分散式鎖 Jun 03, 2023 am 08:16 AM

怎麼在SpringBoot中使用Redis實現分散式鎖

SpringBoot怎麼整合Redisson實現延遲隊列 SpringBoot怎麼整合Redisson實現延遲隊列 May 30, 2023 pm 02:40 PM

SpringBoot怎麼整合Redisson實現延遲隊列

SpringBoot與SpringMVC的比較及差別分析 SpringBoot與SpringMVC的比較及差別分析 Dec 29, 2023 am 11:02 AM

SpringBoot與SpringMVC的比較及差別分析

springboot讀取檔案打成jar包後存取不到怎麼解決 springboot讀取檔案打成jar包後存取不到怎麼解決 Jun 03, 2023 pm 04:38 PM

springboot讀取檔案打成jar包後存取不到怎麼解決

SpringBoot怎麼自訂Redis實作快取序列化 SpringBoot怎麼自訂Redis實作快取序列化 Jun 03, 2023 am 11:32 AM

SpringBoot怎麼自訂Redis實作快取序列化

Springboot+Mybatis-plus不使用SQL語句進行多表新增怎麼實現 Springboot+Mybatis-plus不使用SQL語句進行多表新增怎麼實現 Jun 02, 2023 am 11:07 AM

Springboot+Mybatis-plus不使用SQL語句進行多表新增怎麼實現

springboot怎麼取得application.yml裡值 springboot怎麼取得application.yml裡值 Jun 03, 2023 pm 06:43 PM

springboot怎麼取得application.yml裡值

See all articles