springboot05-SpringBoot 整合Redis

SpringBoot 整合Redis

在springboot2.x之后,原来的jedis被替换为了lettuce

jedis:采用的是直连,多个线程操作的话,是不安全的,如果想要避免不安全,使用jedis pool连接池,更像BIO模式

lettuce:采用netty,实现可以在多个线程中进行共享,不存在线程安全地情况!可以减少线程数据,更像NIO模式

1.导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>

2.配置redis

spring.redis.host=127.0.0.1
spring.redis.port=6379

3.测试

package com.mjh;

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.core.RedisTemplate;

@SpringBootTest
class SpringbootRedisApplicationTests {
    @Autowired
    public RedisTemplate redisTemplate;

   @Test
   void contextLoads() {
       redisTemplate.opsForValue().set("a","111");
   }

}
原文地址:https://www.cnblogs.com/mjjh/p/13295826.html