gpt4 book ai didi

java - Spring Data Redis过期键

转载 作者:IT王子 更新时间:2023-10-29 05:55:40 30 4
gpt4 key购买 nike

我有一个 One Spring Hibernate 应用程序。在我的应用程序中,最近我实现了 Spring 数据 Redis。

spring-servlet.xml
<!-- redis connection factory -->
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"/>

<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnFactory"/>

这个 redisTemplate 在我的 ServiceImpl 类中使用。

RedisServiceImpl

@Autowired
private RedisTemplate<String, T> redisTemplate;

public RedisTemplate<String, T> getRedisTemplate() {
return redisTemplate;
}

public void setRedisTemplate(RedisTemplate<String, T> redisTemplate) {
this.redisTemplate = redisTemplate;
}

现在我像这样在redisServer中添加了数据

public void putData(String uniqueKey, String key, Object results) {

redisTemplate.opsForHash().put(uniqueKey, key, results);
}

现在我想删除过期 key 。

我在谷歌搜索,但谷歌都这样说

redisTemplate.expire(key, timeout, TimeUnit);

在这个expire方法中,我们需要提供uniqueKey而不是key。但是我需要 Expire key 而不是 uniqueKey

那么,请帮助我如何处理过期的 Key

最佳答案

我正在使用 Spring Data Redis。

我正在使用 @Redishash(timeToLive=300) 注释在 300 秒后使我的实体过期。

这是我的 pom.xml

的摘录
...
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
...
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
...
...

我的RedisConfig.class

@Configuration
@Log4j2
@EnableRedisRepositories(basePackageClasses = ConsentOTP.class)
public class RedisConfig {

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

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

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

@Bean
JedisConnectionFactory jedisConnectionFactory() {
log.info("=================================================================");
log.info("redis config : {} : {} ", host, port);
log.info("=================================================================");

RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);
config.setPassword(RedisPassword.of(password));
return new JedisConnectionFactory(config);
}

}

还有我的实体类ConsentOtp.class

@RedisHash(value = "ConsentOTP", timeToLive = 300)
@Data
@NoArgsConstructor
public class ConsentOTP implements Serializable {

private static final long serialVersionUID = 1708925807375596799L;

private String id;
private LocalDateTime timestamp;
private String otp;

public ConsentOTP(String personId, LocalDateTime timestamp, String otp) {
this.id = personId;
this.timestamp = timestamp;
this.otp = otp;
}
}

这是我的 Redis 仓库

public interface ConsentOtpRepository extends CrudRepository<ConsentOTP, String> {

}

关于java - Spring Data Redis过期键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34893279/

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