首頁  >  文章  >  資料庫  >  SpringBoot整合Redis操作API的方法

SpringBoot整合Redis操作API的方法

PHPz
PHPz轉載
2023-05-29 18:19:131032瀏覽

SpringDataRedis呼叫Redis底層解讀

  • 在SpringBoot2.X之前還是直接使用的官方推薦的Jedis連接的Redis

  • #在2 .X之後換為了lettuce

  • SpringBoot整合Redis操作API的方法

    • Jedis:採用直接連接,多線程操作不安全,如果想要避免不安全,使用Jedis pool連接池;BIO

    • lettuce:底層採用Netty,實例可以在多個線程之間共享,不存在線程不安全的情況,可以減少執行緒數量;NIO

SpringBoot整合Redis(原始碼分析)

  • SpringBoot所有的設定類,都有一個自動配置類別

  • 自動配置類別都會綁定一個properties檔案

  • 在原始碼中找到Spring.factories

  • SpringBoot整合Redis操作API的方法

  • #在裡面搜尋redis,找到AutoConfiguration

  • SpringBoot整合Redis操作API的方法

  • SpringBoot整合Redis操作API的方法

  • SpringBoot整合Redis操作API的方法

  • #按ctrl 點選進入類別
  • SpringBoot整合Redis操作API的方法

  • #找到redisproperties.class
  • SpringBoot整合Redis操作API的方法

  • ctrl 點選進入
  • SpringBoot整合Redis操作API的方法

  • 裡面就是全部的redis相關配置了,先簡單看一下,其他的後面再說
  • 預設注入的Bean
  • SpringBoot整合Redis操作API的方法

  • 但是預設的redisTemplate是存在一些問題的,他的key是Object類型的,但是我們期望的一般key都是String類型的這就需要強制類型轉換了,所以上面提出了,可以自己定義RedisTemplate
  • SpringBoot整合Redis操作API的方法

    #在配置配置文件時,如果需要配置連接池,就採用lettuce的,不要直接配置Redis的,配置了也不生效
  • 查看注入時的RedisConnectionFactory
  • SpringBoot整合Redis操作API的方法

  • ##他是存在兩個子類別的,分別是JedisConnectionFactory和LettuceConnectionFactory

  • SpringBoot整合Redis操作API的方法

  • 因為JedisConnectionFactory類別所依賴的很多類別都不存在,所以不能直接使用它

  • SpringBoot整合Redis操作API的方法

  • #全都是爆紅線的,而lettuceConnectionFactory中的依賴就是全部存在的

  • SpringBoot整合Redis操作API的方法

所以所以配置時,採用lettuce的

  • 不要直接設定jedis的

    SpringBoot整合Redis操作API的方法

SpringBoot整合Redis(配置)
  • yml

  • 拷貝properties建立一個yml格式的設定檔, 我還是很喜歡yml的

    SpringBoot整合Redis操作API的方法

  • spring:
      redis:
        host: localhost
        port: 6379
  • Maven

    在项目创建的时候选择,如果没有选择就添加
    <dependency>
      <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    SpringBoot整合Redis操作API的方法#編寫測試

  • 開啟SpringBoot預設建立的測試類別

  • ############redisTemp操作資料類型的方法都是以opsFor開頭,後面是類型############ ###############例如opsForValue就是操作字串的#############然後後面的應用程式就跟前面寫的API一樣了###
  • SpringBoot整合Redis操作API的方法

  • 常用的操作可以直接点就可以了

  • 关于事物的

redisTemplate.unwatch();
redisTemplate.watch("key");
redisTemplate.multi();
redisTemplate.discard();
redisTemplate.exec();

关于数据库的操作需要获取链接后使用连接对象操作

RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();connection.flushAll();connection.flushDb();connection.close();

测试代码及其执行结果

package co.flower.redis02springboot;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTestclass Redis02SpringbootApplicationTests {/** * 我居然直接就指定了泛型 RedisTemplate<String,Object>结果就直接报错了,删除泛型后成功     */@Autowiredprivate RedisTemplate redisTemplate;

    @Testvoid contextLoads() {// 英文测试redisTemplate.opsForValue().set("name","xiaojiejie");
        System.out.println(redisTemplate.opsForValue().get("name"));// 中文测试redisTemplate.opsForValue().set("name","小姐姐");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }

}

执行结果,SpringBoot的启动加载和结束销毁没有粘贴/***SpringBootStart****/xiaojiejie
小姐姐/***SpringBootStop*****/

以上是SpringBoot整合Redis操作API的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除