RedisTemplate 的 Boolean expire(K key, long timeout, TimeUnit unit) 方法(Spring类 )

文档地址 https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTemplate.html#expire(K,long,java.util.concurrent.TimeUnit)

expire

public Boolean expire(K key, long timeout, TimeUnit unit)

Description copied from interface: RedisOperations

设置给定密钥的生存时间。
Set time to live for given key.

Specified by:
expire in interface RedisOperations<K,V>

Parameters:

  • key - must not be null.
  • unit - must not be null.

Returns:
null when used in pipeline / transaction.

示例代码

stringRedisTemplate.expire(ONE_LEVERLIMIT_KEY + phone, 5, TimeUnit.MINUTES);

使用了 Spring 提供的 StringRedisTemplate 类的 expire 方法,其参数含义如下:

  • ONE_LEVERLIMIT_KEY + phone:构造 Redis 中的键名,通常用于标识特定手机号的一级限制状态。
  • 5:设置的过期时间数值。

因此,该行代码的作用是将键 ONE_LEVERLIMIT_KEY + phone 的过期时间设置为 5 分钟。在这段时间内,该键存在于 Redis 中,表示对应的手机号处于一级限制状态;5 分钟后,键自动过期,手机号的限制状态解除。

posted @ 2025-04-11 20:37  kuki'  阅读(79)  评论(0)    收藏  举报