Redis初步学习整理——第四节SpringBoot集成Redis

前言

其实StringBoot继承Redis是非常简单的,因为StringBoot本身就是为了简化各种配置而设置的,但是需要知道的是连接Redis的底层是Jedis,这个也是Redis推荐的连接中间件,所以要学习肯定是学习全套,首先让我们探索一下如何通过Jedis连接Redis以及如何使用Jedis,然后再看一下SpringBoot如何配置Redis!

一、Jedis

就像前言讲的一样,Jedis就是java连接Redis的中间件,其中的命令和Redis命令完全是一致的,所以这里也没必要重复写这些命令的必要了,只要引入下边这个依赖就可以了。

        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.3.0</version>
        </dependency>

当然也可以直接查看我的GitHub项目,都是一些练习的命令

二、SpringBoot集成Redis

SpringBoot集成Redis,需要通过一个依赖SpringData,其实不管是集成Redis,还是mysql、MongoDB都是通过SpringData来进行的
在这里插入图片描述
可以看到官网文档中是对很多进行了集成,另外还有一点需要注意的是,在SpringData中和Redis进行连接的并不是Jedis,而是Lettuce(SpringBoot2.x之后),这两有啥区别呢?
Jedis: 采用的直连Redis的方式,多个线程操作的话,是不安全的,当然可以使用Redis pool的方式!它的运行模式更像 BIO模式
Lettuce: 采用netty, 实例可以在多个线程中进行同享,不存在线程不安全的情况,可以减少线程数据,更像 NIO 模式

BIO模式:同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通过线程池机制改善。
NIO模式:同步非阻塞,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理。
整理自Java BIO、NIO、AIO 学习

关于SpringBoot 集成Redis,非常自动化,已经简单到不需要懂什么都可以实现了。

(1)引入依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

(2)配置文件

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123

(3)测试代码

@SpringBootTest
class RedisSpringbootApplicationTests {
   

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 在RedisTemplate中使用的不是Jedis,而是Lettuce,Lettuce使用的是netty
     */
    @Test
    void contextLoads() {
   
        /* opsForValue() 操作String类型 */
        redisTemplate.opsForValue().set("name","cainiao");
        System.out.println(redisTemplate.opsForValue().get("name"));
        System.out.println(redisTemplate.keys("*"));
        /* opsForZSet 操作Zset类型 */
        redisTemplate.opsForZSet().add("zset","v0",0);
        System.out.println(redisTemplate.opsForZSet().rangeByScoreWithScores("zset",0,10));
        /* opsForGeo 操作Geo类型 , 如果使用的是windows的redis,注意一下该版本是否提供了对三大特殊类型的支持 */
//        redisTemplate.opsForGeo().add("geos",new RedisGeoCommands.GeoLocation("beijing",new Point(116.23128,40.22077)));
//        System.out.println(redisTemplate.opsForGeo().position("geos","beijing"));
        /* opsForSet 操作set类型 */
        redisTemplate.opsForSet().add("set","v0","v1","v2","v3");
        System.out.println(redisTemplate.opsForSet().members("set"));
        /* opsForHash 操作Hash类型 */
        redisTemplate.opsForHash().put("hash","k1","v1");
        System.out.println(redisTemplate.opsForHash().get("hash","k1"));
        /* opsForList 操作List类型 */
        redisTemplate.opsForList().rightPush("list","v0");
        System.out.println(redisTemplate.opsForList().rightPop("list"));
        
        /* 其他操作方式都和Jedis大同小异,不再多练习了 */
    }
}

三、自定义RedisTemplate及工具类

一般在工作中使用到Redis的时候都是对RedisTemplate进行了再次封装得到的一个工具类,这样我们使用起来也是可以简化开发的,而且原有的RedisTemplate的序列化方式是JDK的默认序列化方式,存储到redis中是转义字符的,所以我们也需要自定义一个RedisTemplate的序列化方式

(1)自定义RedisTemplate


/**
 * @program: redis-demo
 * @description: Redis配置类
 **/
@Configuration
public class RedisConfig {
   


    @Bean("redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
   
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        jackson2JsonRedisSerializer.setObjectMapper(objec
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值