springboot redis多數(shù)據(jù)源怎樣設(shè)置過期時(shí)間

小樊
81
2024-11-11 18:40:28
欄目: 云計(jì)算

在Spring Boot中配置Redis多數(shù)據(jù)源并設(shè)置過期時(shí)間,你需要遵循以下步驟:

  1. 添加依賴

確保你的pom.xml文件中包含了Spring Boot Redis的依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置數(shù)據(jù)源

application.ymlapplication.properties文件中配置多個(gè)數(shù)據(jù)源。例如:

# application.yml
spring:
  redis:
    datasource1:
      host: localhost
      port: 6379
      password: your_password
      database: 0
      timeout: 60000
    datasource2:
      host: localhost
      port: 6380
      password: your_password
      database: 1
      timeout: 60000
  1. 創(chuàng)建數(shù)據(jù)源配置類

創(chuàng)建一個(gè)配置類,用于創(chuàng)建和配置多個(gè)Redis數(shù)據(jù)源。例如:

@Configuration
public class RedisDataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.redis.datasource1")
    public DataSource redisDataSource1() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis.datasource2")
    public DataSource redisDataSource2() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        return template;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate2(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        return template;
    }
}
  1. 創(chuàng)建Redis配置類

創(chuàng)建一個(gè)配置類,用于配置多個(gè)數(shù)據(jù)源的RedisTemplate。例如:

@Configuration
public class RedisConfig {

    @Autowired
    private RedisDataSourceConfig redisDataSourceConfig;

    @Bean
    public RedisTemplate<String, Object> redisTemplate1() {
        RedisTemplate<String, Object> template = redisDataSourceConfig.redisTemplate1(redisDataSourceConfig.redisDataSource1());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate2() {
        RedisTemplate<String, Object> template = redisDataSourceConfig.redisTemplate2(redisDataSourceConfig.redisDataSource2());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}
  1. 設(shè)置過期時(shí)間

在你的服務(wù)類中,使用RedisTemplate設(shè)置過期時(shí)間。例如:

@Service
public class MyService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate1;

    public void setKeyWithExpiration(String key, Object value, int expiration) {
        redisTemplate1.opsForValue().set(key, value, expiration, TimeUnit.SECONDS);
    }
}

在這個(gè)例子中,我們?yōu)榈谝粋€(gè)數(shù)據(jù)源設(shè)置了過期時(shí)間為expiration秒。你可以根據(jù)需要調(diào)整數(shù)據(jù)源和過期時(shí)間。

0