Redis整合SpringBoot使用(Windows)

导读:本篇文章讲解 Redis整合SpringBoot使用(Windows),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

  1. 下载依赖
     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  1. 配置yml
  2. spring:  
      redis:
        host: 127.0.0.1
        port: 6379
        database: 0  #默认有十六个数据库
        #Redis连接池配置
        jedis:
          pool:
            max-active: 8 #最大连接数
            max-wait: 1ms #连接池最大阻塞等待时间
            max-idle: 4 #连接池中的最大空闲连接
            min-idle: 0 #连接池中的最小空闲连接

  3. 新增配置类
  4. @Configuration
    public class RedisConfig extends CachingConfigurerSupport {
    
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
            RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
    
            //默认的key序列化器为:JdkSerializationRedisSerializer
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setConnectionFactory(connectionFactory);
            return redisTemplate;
        }
    }

  5. 操作数据
  6. //添加数据   5分钟的过期时间
    redisTemplate.opsForValue().set(email, code, 5, TimeUnit.MINUTES);
    
    //根据Key进行获取
     Object attribute = redisTemplate.opsForValue().get(userBo.getEmail());

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/105084.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!