Spring Data Redis 过期密钥 [英] Spring Data Redis Expire Key
问题描述
我有一个 One Spring Hibernate 应用程序.在我的应用程序中,最近我实现了 Spring data Redis.
I have a One Spring Hibernate Application. In my application, Recently i am implemented Spring data 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 类中使用.
And this redisTemplate
use in my ServiceImpl class.
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中添加数据
Now I added data in redisServer like this
public void putData(String uniqueKey, String key, Object results) {
redisTemplate.opsForHash().put(uniqueKey, key, results);
}
现在我想删除过期键.
我在谷歌搜索,但在谷歌上都是这样说的
I search in Google, But in google all are saying like this
redisTemplate.expire(key, timeout, TimeUnit);
在这个expire方法中,我们需要提供uniqueKey
而不是key
.但我需要过期 key
而不是 uniqueKey
.
In this expire method, We need to provide uniqueKey
instead of key
.
But I need to Expire key
instead of uniqueKey
.
所以请帮我看看过期的Key
怎么办?
So Please help me what can i do for expire Key
?
推荐答案
我正在使用 Spring Data Redis.
I am using Spring Data Redis.
我正在使用 @Redishash(timeToLive=300)
注释在 300 秒后使我的实体过期.
I am using @Redishash(timeToLive=300)
annotation to expire my Entities after 300 seconds.
这是我的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存储库
Here is my Redis repository
public interface ConsentOtpRepository extends CrudRepository<ConsentOTP, String> {
}
这篇关于Spring Data Redis 过期密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!