redistemplate list设置过期时间
时间: 2023-08-15 09:13:06 浏览: 331
您可以使用 `expire` 方法来为 RedisTemplate 中的 List 设置过期时间。以下是示例代码:
```java
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.beans.factory.annotation.Autowired;
public class ExampleService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void setListWithExpiration(String key, Object value, long expirationInSeconds) {
ListOperations<String, Object> listOps = redisTemplate.opsForList();
listOps.leftPush(key, value);
redisTemplate.expire(key, expirationInSeconds, TimeUnit.SECONDS);
}
}
```
在上述示例中,我们首先通过 `redisTemplate` 获取到 List 的操作对象 `listOps`,然后使用 `listOps.leftPush()` 方法向 List 中添加元素。最后,我们使用 `redisTemplate.expire()` 方法设置键的过期时间,单位为秒。
您可以根据您的实际需求调整代码中的参数和方法调用。希望这能帮助到您!
阅读全文
相关推荐


















