>  기사  >  데이터 베이스  >  SpringBoot가 Redis 작업 API를 통합하는 방법

SpringBoot가 Redis 작업 API를 통합하는 방법

PHPz
PHPz앞으로
2023-05-29 18:19:131027검색

SpringDataRedis는 SpringBoot2 이전의 Redis

  • 의 기본 해석을 호출합니다.

  • Jedis: 다중 스레드 작업은 안전하지 않음을 피하려면 BIO
  • lettuce를 사용하세요. Netty는 하위 계층으로 사용되며 여러 스레드 간에 인스턴스를 공유할 수 있습니다. 안전을 위해 스레드 수를 줄일 수 있습니다. NIO
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • SpringBoot는 Redis를 통합합니다.
    • SpringBoot의 모든 구성 클래스에는 자동 구성 클래스가 있습니다.

    • 자동 구성 클래스는 소스 코드에서 File

    Find Spring.factories

  • 속성에 바인딩됩니다. , redis를 검색하고 AutoConfiguration

  • ctrl+클릭을 눌러 클래스에 들어가세요
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • redisproperties.class
  • SpringBoot가 Redis 작업 API를 통합하는 방법 찾기

  • ctrl+ 클릭해서 들어가세요
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • redis 관련 설정이 모두 있으니 먼저 간단히 살펴보고 나머지는 나중에 다루겠습니다
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • 기본적으로 주입되는 Bean
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • 그러나 기본 redisTemplate에는 몇 가지 문제가 있습니다. 키는 Object 유형이지만 우리가 기대하는 일반 키는 필수 유형이 필요한 String 유형이므로 위에서 언급한 대로 RedisTemplate을 정의할 수 있습니다. by yourself
  • SpringBoot가 Redis 작업 API를 통합하는 방법

    구성 파일을 구성할 때 연결 풀을 구성해야 한다면 Lettuce를 사용하세요. Redis를 직접 구성하지 마세요.
  • View 주입 RedisConnectionFactory에는 두 가지 하위 클래스가 있습니다.
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • , 즉 JedisConnectionFactory 및 LettuceConnectionFactory
  • . 많은 클래스가 존재하지 않으므로 직접 사용할 수 없습니다.
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • 는 모두 빨간색 선으로 표시되어 있고 lettuceConnectionFactory의 종속성은 모두 있습니다.
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • 구성할 때 lettuce의
  • SpringBoot가 Redis 작업 API를 통합하는 방법

    을 사용하세요.
  • jedis를 구성하지 마세요. 직접
  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • SpringBoot는 Redis(구성)를 통합합니다
  • ymlSpringBoot가 Redis 작업 API를 통합하는 방법

  • 속성을 복사하여 yml 형식 구성 파일을 생성합니다. 저는 여전히 yml을 매우 좋아합니다

  • SpringBoot가 Redis 작업 API를 통합하는 방법

  • spring:
      redis:
        host: localhost
        port: 6379
M aven

在项目创建的时候选择,如果没有选择就添加
<dependency>
  <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

쓰기 test

  • 기본적으로 SpringBoot에서 생성된 테스트 클래스를 엽니다.

  • SpringBoot가 Redis 작업 API를 통합하는 방법

redisTemp 데이터 유형을 조작하는 방법은 모두 opsFor로 시작하고 그 뒤에 유형

  • 예를 들어 , opsForValue는 문자열에서 작동합니다
  • SpringBoot가 Redis 작업 API를 통합하는 방법

    이후 적용은 이전에 작성한 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으로 문의하시기 바랍니다. 삭제