SpringBoot整合Redis

一、代码

spring-boot版本

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.3.12.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

pom.xml

<dependencies>
   ...
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
   </dependency>
   <!-- spring2.X集成redis所需common-pool2-->
   <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.6.0</version>
   </dependency>
</dependencies>

application.yml

spring:
  ...
  redis:
    host: 127.0.0.1
    port: 6379
    database: 0  #Redis使用的数据库
    timeout: 18000   #连接超时事件毫秒
    lettuce:
      pool:
        max-active: 20   #连接池最大连接数
        max-idle: 5   #最大阻塞等待时间
        min-idle: 0  #连接池最小空闲连接

RedisConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    /**
     * redis序列化的工具配置类
     * 127.0.0.1:6379 keys *
     * 1)"ord:102" 序列化过
     * 2)"\xac\xed\x00\x05t\ord:102"  没有序列化过
     * this.redisTemplate.opsForValue()//提供了操作String类型的所有方法
     * this.redisTemplate.opsForList()//提供了操作List类型的所有方法
     * this.redisTemplate.opsForSet()//提供了操作Set类型的所有方法
     * this.redisTemplate.opsForHash()//提供了操作Hash类型的所有方法
     * this.redisTemplate.opsForZSet()//提供了操作ZSet类型的所有方法
     *
     * @param lettuceConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        //设置key序列化方式String
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认的序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

TestController.java

@RestController
public class TestController {
    
    ...
    @Autowired
    RedisTemplate redisTemplate;

    ...
    @RequestMapping(value = "/test/getRedisValue", method = RequestMethod.GET)
    public Object getRedisValue(){
        redisTemplate.opsForValue().set("name", "seven");
        Object obj = redisTemplate.opsForValue().get("name");
        return obj;
    }

}

二、效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术宅老谢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值