gpt4 book ai didi

spring-boot - 如何使用 spring boot 2.0 配置 redis ttl

转载 作者:行者123 更新时间:2023-12-03 09:31:37 26 4
gpt4 key购买 nike

我在 spring boot 2.0 中使用 redis。

我希望将存储在 redis 中的数据设置为 TTL。

于是找了个办法,写了下面的示例代码。

人.java

@Data
@Builder
@RedisHash("Person")
public class Person implements Serializable {

@Id
private Long id;

private PersonInfo info;
}

个人信息.java
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PersonInfo implements Serializable {

private String name;

private Long age;
}

RedisConfig.java
@Configuration
@Slf4j
public class RedisConfig extends CachingConfigurerSupport {

@Value("${spring.redis.host}")
private String redisHost;

@Value("${spring.redis.port}")
private int redisPort;

@Value("${spring.redis.expiretime}")
private Long expireTime;



@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {

log.info("Info -> Lettuce Connection Factory");

LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
lettuceConnectionFactory.setHostName(this.redisHost);
lettuceConnectionFactory.setPort(this.redisPort);

log.info("RedisHost -> " + lettuceConnectionFactory.getHostName());
log.info("RedisPort -> " + lettuceConnectionFactory.getPort());
log.info("ExpireTime -> " + expireTime);

return lettuceConnectionFactory;
}


@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {

log.info("Info -> Redis Template");

RedisTemplate redisTemplate = new RedisTemplate();

redisTemplate.setKeySerializer(new StringRedisSerializer());


redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}



@Bean
public RedisCacheConfiguration cacheConfiguration() {

log.info("Info -> Redis Cache Configuration");

RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(expireTime))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
cacheConfig.usePrefix();

log.info("Duration -> " + cacheConfig.getTtl().getSeconds());

return cacheConfig;
}


@Bean
public RedisCacheManager cacheManager() {

log.info("Info -> Redis Cache Manager");

RedisCacheManager rcm = RedisCacheManager
.builder(this.lettuceConnectionFactory())
.cacheDefaults(this.cacheConfiguration())
.build();

return rcm;
}
}

PersonRedisRepository
public interface PersonRedisRepository extends CrudRepository<Person, Long> {}

个人 Controller .java
@RestController
@Slf4j
public class PersonController {

@Autowired
private PersonRedisRepository personRedisRepository;



@PostMapping("/person/{id}")
public Person createPerson(@PathVariable("id") Long id) {

log.info("Info -> CreatePerson START ================= ");

Person person = Person.builder()
.id(id)
.info(PersonInfo.builder()
.name("Test -> " + id)
.age(id + 15)
.build()
)
.build();

personRedisRepository.save(person);

Person getPerson = personRedisRepository.findById(id).get();
log.info("Info -> getPerson : " + getPerson.toString());

log.info("Info -> createPerson END ================= ");

return getPerson;
}



@GetMapping("/person/{id}")
public Person findPerson(@PathVariable("id") Long id) {

log.info("Info -> FindPerson START ================= ");

Person findedPerson = personRedisRepository.findById(id).get();
log.info("Info -> findedPerson : " + findedPerson.toString());

log.info("Info -> FindPerson END ================= ");

// return findedPerson;
return null;
}
}

应用程序.yml
spring:
# Docker Redis
redis:
host: 192.168.99.100
port: 6379
expiretime: 100

server:
port: 8083

我编写了 RedisConfig.java 和其他代码并进行了如下测试。

我用 Rest Client 来请求

要求
GET http://localhost:8083/person/7 HTTP/1.1
redis:6379> keys *
1) "Person"
2) "Person:3"
redis:6379> hgetall Person:3
1) "_class"
2) "my.cachemanager.redis.person.domain.Person"
3) "id"
4) "3"
5) "info.name"
6) "Test -> 3"
7) "info.age"
8) "18"
redis:6379> TTL Person:3
(integer) -1

我确认数据是通过redis-cli存储在redis中的。
但是,我也确认未应用 ttl 时间。

如何将 TTL 应用于存储在 Redis 中的数据?

我将衷心感谢您的帮助。

最佳答案

您可以使用 @TimeToLive注释(在 documentation 的第 8.7 节中提到。它可以应用于属性或方法,(不应应用于同一类的两者)

关于spring-boot - 如何使用 spring boot 2.0 配置 redis ttl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56414461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com