Spring Boot整合redis

1.搭建最简单的springboot环境

        (1)引入依赖,只含有简单的启动器

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.4.4</version>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

        (2)编写配置文件,这里若不配置,默认的端口号就是8080

server:
  port: 8081

        (3)编写启动类

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

        (4)检验,可以编写一个简单的程序来验证环境是否搭建成功,如下:访问localhost:8081/hello              可以输出Hello World,就代表平台搭建成功

@RestController
public class Hello {

    @GetMapping("hello")
    public String hello() {
        return "Hello World";
    }
}

2.整合redis

        (1)在已经搭建好简单的SpringBoot的情况下导入新的依赖(redis与jedis连接池的依                             赖),如果不想使用jedis连接池可以不用导入jedis的依赖

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

        (2)编写配置文件:在原有配置文件的基础上添加下方配置(如果不使用jedis,将关于                          jedis的配置去掉,而且spring-data-redis-client-type一定要去掉,否则会报错)

spring:
  data:
    redis:
      host: 192.168.75.128
      port: 6379
      database: 10
      client-type: jedis
      jedis:
        pool:
          enabled: true
          max-active: 8
          min-idle: 0
          max-wait: -1

        (3)编写配置类,对写入redis的键和值进行序列化与定义缓存策略

@EnableCaching
@Configuration
public class RedisConfig { // 不再继承 CachingConfigurerSupport

    // 配置 RedisTemplate,定义 Key-Value 序列化规则
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer()); // Key 序列化
        // 使用 GenericJackson2JsonRedisSerializer 简化配置,避免弃用方法
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash Value 序列化
        template.setConnectionFactory(factory);
        return template;
    }

    // 配置 CacheManager,定义缓存策略(如过期时间、序列化规则)
    @Bean
    public org.springframework.cache.CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600)) // 缓存过期时间 600 秒
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) // Key 序列化
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) // Value 序列化,使用新序列化类
                .disableCachingNullValues(); // 禁用空值缓存
        return RedisCacheManager.builder(factory).cacheDefaults(config).build();
    }
}

        (4)测试:访问localhost:8081/hello1后输出Success!!!然后进入redis中查看是否插入成功,                     进一步检验

@RestController
public class Hello {

    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("hello1")
    public String hello1() {
        redisTemplate.opsForValue().set("hello", "world");
        return "Success!!!";
    }
}

3.注意:如果使用连接池的话,不用自己进行配置,springboot会贴心的自动帮你配置好,只需要在配置文件中指定关于jedis的一些参数即可,但是jedis的依赖与spring-data-redis-client-type=jedis都不能少,任何一处缺少都会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值