【Redis】整合springboot

Redis SpringBoot整合整合步骤原理jedis和lettuce的区别RedisReactiveAutoConfigurationRedisProperties自定义RedisTemplate序列化方式RedisTemplate代码测试RedisUtils整合步骤创建springboot项目时找到nosql 里的redis 勾选上修改配置 在application.properties,可以查看 RedisProperties分析测试SpringBoot主要是使用RedisTempla
摘要由CSDN通过智能技术生成

整合步骤

  1. 创建springboot项目时找到nosql 里的redis 勾选上
  2. 修改配置 在application.properties,可以查看 RedisProperties分析
  3. 测试
    SpringBoot主要是使用RedisTemplate类进行各种操作

在这里插入图片描述

原理

在Springboot2以后,底层访问redis不再是jedis,而是lettuce。

jedis和lettuce的区别

1. jedis
jedis采用的是直连redis server,在多个线程之间共用一个Jedis实例时,是线程不安全的。如果想避免线程不安全,可以使用连接池pool,这样每个线程单独使用一个jedis实例。这样带来的问题就是,如果线程数过多,带来的redis server负载会加大,有点类似于BIO的模式。
2. lettuce
lettuce采用netty连接redis server,实例可以在多个线程之间共享,不存在线程不安全的情况,这样可以减少线程数量。
在特殊情况下,lettuce也可以使用多个实例,有点类似于NIO模式

RedisReactiveAutoConfiguration

springboot的autoconfiguration中有redis的自动配置类
在这里插入图片描述

在这里插入图片描述

RedisProperties

在这里插入图片描述
可以根据RedisProperties的属性 在application.properties进行相应的配置
在这里插入图片描述

自定义RedisTemplate

从RedisReactiveAutoConfiguration的上面我们看到

    @ConditionalOnMissingBean(
        name = {
   "redisTemplate"}
    )

所以当我们自己实现了RedisTemplate,就不会用默认的了

RedisTemplate<String, Object> template = new RedisTemplate();

序列化方式

我们可以通过template.setKeySerializer() 方法的参数RedisSerializer
在这里插入图片描述

看到有这么多序列化方式:

在这里插入图片描述

我们只需new 出具体的序列化的对象,就可以选择我们想要的序列化方式

RedisTemplate代码

@Configuration
public class RedisConfig {
   

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
   
        // 修改1 :直接使用<String, Object>
        RedisTemplate<String, Object> template = new RedisTemplate();
        //连接工厂
        template.setConnectionFactory(redisConnectionFactory);

        // 修改2:Json序列化配置
        Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();//转义
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectJackson2JsonRedisSerializer.setObjectMapper(om);
        //String 的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        //hash的key采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        //value 序列化方式采用jackson
        template.setValueSerializer(objectJackson2JsonRedisSerializer);
        // hash value序列化方式采用jackson
        template.setHashValueSerializer(objectJackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

测试

测试时 注意要用我们自己的RedisTemplate
可以使用 @Qualifier(“redisTemplate”) 注解指定

@SpringBootTest
class RedisSpringbootApplicationTests {
   


    @Autowired
    @Qualifier("redisTemplate") //重名指定
    private RedisTemplate redisTemplate;

    @Test
    public void test() throws JsonProcessingException {
   
        User user = new User("zxf", 3);
        //String jsonUser = new ObjectMapper().writeValueAsString(user);
        redisTemplate.opsForValue().set("user",user);
        System.out.println(redisTemplate.opsForValue().get("user"));

    }

}

RedisUtils

真实开发中, 一般要写RedisUtils类
具体在狂神26的15分钟

package com.flora.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author flora
 * @create 2021-01-06 13:39
 */
@Component
public final class RedisUtil {
   



        @Autowired
        private RedisTemplate<String, Object> redisTemplate;

        // =============================common============================
        /**
         * 指定缓存失效时间
         * @param key  键
         * @param time 时间(秒)
         */
        public boolean expire(String key, long time) {
   
            try {
   
                if (time > 0) {
   
                    redisTemplate.expire(key, time, TimeUnit.SECONDS)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值