Redis中EXPIREAT命令实现
EXPIREAT 用于为 Key 设置以 Unix 时间戳(秒)为单位的绝对过期时间,到期后 Redis 会自动删除该 Key。与 EXPIRE 设置相对时间(从现在起多少秒后过期)不同,EXPIREAT 指定的是一个绝对时间点,适用于需要在特定日期或时刻让 Key 失效的场景。
本文基于 Redis 通用 Key 操作介绍 EXPIREAT。Redis 命令本身不区分大小写,因此 EXPIREAT、expireat 和 Expireat 的效果相同;文档统一使用大写形式。EXPIREAT 自 Redis 1.2 起可用,条件选项 NX、XX、GT、LT 从 Redis 7.0 开始支持。
一、命令概览
1. 基本语法
EXPIREAT key timestamp [NX | XX | GT | LT]
参数说明:
| 参数 | 说明 |
|---|---|
| key | 要设置过期时间的 Key |
| timestamp | Unix 时间戳(秒),绝对过期时间点 |
| NX | 仅当 Key 没有过期时间时才设置(Not eXists),Redis 7.0+ |
| XX | 仅当 Key 已有过期时间时才设置(eXists),Redis 7.0+ |
| GT | 仅当新过期时间戳大于当前过期时间戳时才设置(Greater Than),Redis 7.0+ |
| LT | 仅当新过期时间戳小于当前过期时间戳时才设置(Less Than),Redis 7.0+ |
返回值:
- 设置成功返回 (integer) 1。
- 设置失败返回 (integer) 0(如 Key 不存在、条件不满足等)。
2. 最简单的示例
先写入一个字符串值,然后设置在指定 Unix 时间戳过期:
SET mykey "Hello" EXPIREAT mykey 1735689600
返回:
(integer) 1
查看剩余生存时间:
TTL mykey
返回值为当前时间到 1735689600 的剩余秒数(正数)。
查看过期时间戳:
EXPIRETIME mykey
返回:
(integer) 1735689600
二、Unix 时间戳基础
1. 什么是 Unix 时间戳
Unix 时间戳是从 UTC 1970 年 1 月 1 日 00:00:00 起经过的秒数。EXPIREAT 接受的就是这种秒级 Unix 时间戳。
常见时间点的 Unix 时间戳(示例):
| 时间(UTC) | Unix 时间戳 |
|---|---|
| 2025-01-01 00:00:00 | 1735689600 |
| 2025-06-01 00:00:00 | 1748736000 |
| 2026-01-01 00:00:00 | 1767225600 |
| 2026-08-28 00:00:00 | 1787548800 |
2. 获取当前 Unix 时间戳
在 redis-cli 中可以用 TIME 命令获取服务器当前时间:
TIME
返回类似:
1) "1724719123" 2) "456789"
第一个值为秒级时间戳,第二个值为微秒部分。
在 Shell 中获取:
date +%s
3. 时间戳计算
可以结合 Shell 命令计算未来的时间戳。例如设置 1 小时后过期:
redis-cli EXPIREAT mykey $(( $(date +%s) + 3600 ))
设置明天同一时间过期:
redis-cli EXPIREAT mykey $(( $(date +%s) + 86400 ))
三、EXPIREAT 的基本用法
1. 设置绝对过期时间
SET promo:coupon:1001 "DISCOUNT50" EXPIREAT promo:coupon:1001 1767225600
返回 (integer) 1,该 Key 将在 UTC 2026-01-01 00:00:00 过期。
2. 对不存在的 Key 设置过期
EXPIREAT no-such-key 1735689600
返回:
(integer) 0
Key 不存在时,EXPIREAT 返回 0,不会创建 Key。
3. 时间戳在过去(立即过期)
传入的时间戳小于当前时间戳时,Key 会立即过期(相当于被删除):
SET temp "value" EXPIREAT temp 1 EXISTS temp
EXISTS temp 返回 (integer) 0。
利用这一特性可以实现"立即删除"的效果,但通常应使用 DEL 或 UNLINK。
4. 时间戳为 0
Unix 时间戳 0 对应 1970-01-01 00:00:00 UTC,显然在过去,Key 会立即过期:
SET temp "value" EXPIREAT temp 0 EXISTS temp
EXISTS temp 返回 (integer) 0。
5. 负值时间戳
传入负值时间戳时,Key 也会立即过期:
SET temp "value" EXPIREAT temp -1 EXISTS temp
EXISTS temp 返回 (integer) 0。
四、条件选项(NX / XX / GT / LT)
Redis 7.0 及以上版本支持条件选项,用于更精细地控制过期时间的设置行为。
1. NX — 仅当没有过期时间时设置
NX(Not eXists):只有当 Key 当前没有关联过期时间(即永久有效)时,才设置新的过期时间。
SET permanent-key "forever" EXPIREAT permanent-key 1735689600 NX
返回 (integer) 1,因为该 Key 原本没有过期时间。
如果 Key 已有过期时间:
SET temp-key "temp" EX 60 EXPIREAT temp-key 1735689600 NX
返回 (integer) 0,因为 NX 条件不满足。
2. XX — 仅当已有过期时间时设置
XX(eXists):只有当 Key 当前已有过期时间时,才更新为新的过期时间。
SET temp-key "temp" EX 60 EXPIREAT temp-key 1735689600 XX
返回 (integer) 1,因为该 Key 已有过期时间,成功更新。
如果 Key 是永久有效的:
SET permanent-key "forever" EXPIREAT permanent-key 1735689600 XX
返回 (integer) 0,因为 XX 条件不满足。
3. GT — 仅当新过期时间更晚时设置
GT(Greater Than):只有当新的过期时间戳大于当前剩余过期时间戳时,才更新过期时间。Redis 7.0+ 支持。
SET mykey "value" EX 100 EXPIREAT mykey 1735689600 GT
如果 1735689600 对应的过期时间在当前过期时间之后,返回 (integer) 1,过期时间被延长。
如果新过期时间更早:
SET mykey "value" EXAT 1735689600 EXPIREAT mykey 1000 GT
时间戳 1000 对应 1970-01-01,远在当前过期时间之前,返回 (integer) 0。
4. LT — 仅当新过期时间更早时设置
LT(Less Than):只有当新的过期时间戳小于当前剩余过期时间戳时,才更新过期时间。Redis 7.0+ 支持。
SET mykey "value" EXAT 1735689600 EXPIREAT mykey 1735660000 LT
如果 1735660000 < 1735689600,返回 (integer) 1,过期时间被缩短。
如果新过期时间更晚:
SET mykey "value" EX 100 EXPIREAT mykey 1735689600 LT
如果 1735689600 对应的过期时间在当前过期时间之后,返回 (integer) 0。
5. 选项约束
NX 与 XX 互斥,GT 与 LT 互斥。GT 和 LT 不能与 NX 同时使用(没有当前过期时间时无法比较大小)。
五、EXPIREAT 与相关命令的区别
1. EXPIREAT 与 EXPIRE
EXPIREAT 设置绝对过期时间(Unix 时间戳),EXPIRE 设置相对过期时间(从现在起的秒数):
EXPIRE key 3600 # 3600 秒后过期 EXPIREAT key 1735689600 # 在指定 Unix 时间戳过期
两者可以互相转换:EXPIREAT key timestamp 等价于 EXPIRE key (timestamp - 当前时间戳)。
需要在特定时间点过期时用 EXPIREAT,需要"多少秒后"过期时用 EXPIRE。
2. EXPIREAT 与 PEXPIREAT
EXPIREAT 接受秒级时间戳,PEXPIREAT 接受毫秒级时间戳:
EXPIREAT key 1735689600 # 秒级时间戳 PEXPIREAT key 1735689600000 # 毫秒级时间戳
两者语义相同,精度不同。需要毫秒级精度时使用 PEXPIREAT。
3. EXPIREAT 与 SET … EXAT
SET 命令(Redis 6.2+)可以通过 EXAT 选项在设置值的同时指定绝对过期时间戳,是原子操作:
SET cache:key "value" EXAT 1735689600
等效于(但更高效且原子):
SET cache:key "value" EXPIREAT cache:key 1735689600
推荐优先使用 SET ... EXAT 形式,避免 SET 成功但 EXPIREAT 失败导致 Key 永久有效的风险。
4. EXPIREAT 与 EXPIRETIME / PEXPIRETIME
EXPIRETIME 返回 Key 过期时间对应的 Unix 时间戳(秒),Redis 7.0+ 支持:
SET mykey "value" EXAT 1735689600 EXPIRETIME mykey
返回:
(integer) 1735689600
| 状态 | EXPIRETIME 返回值 |
|---|---|
| Key 存在且有过期时间 | 过期时间戳(正数) |
| Key 存在但无过期时间 | -1 |
| Key 不存在 | -2 |
5. EXPIREAT 与 PERSIST
PERSIST 用于移除 Key 的过期时间,使其变为永久有效:
SET temp-key "value" EXAT 1735689600 PERSIST temp-key TTL temp-key
TTL temp-key 返回 -1(永久有效)。
6. 相关命令总览
| 命令 | 时间类型 | 精度 | 说明 |
|---|---|---|---|
| EXPIRE key seconds | 相对 | 秒 | 多少秒后过期 |
| PEXPIRE key ms | 相对 | 毫秒 | 多少毫秒后过期 |
| EXPIREAT key timestamp | 绝对 | 秒 | 在指定时间戳过期 |
| PEXPIREAT key timestamp | 绝对 | 毫秒 | 在指定毫秒时间戳过期 |
| TTL key | — | 秒 | 剩余秒数 |
| PTTL key | — | 毫秒 | 剩余毫秒数 |
| EXPIRETIME key | — | 秒 | 过期时间戳(秒) |
| PEXPIRETIME key | — | 毫秒 | 过期时间戳(毫秒) |
| PERSIST key | — | — | 移除过期时间 |
六、过期时间的刷新与更新
1. 覆盖式更新
对已有过期时间的 Key 再次执行 EXPIREAT,会覆盖原有的过期时间:
SET mykey "value" EXAT 1735689600 EXPIREAT mykey 1767225600 EXPIRETIME mykey
EXPIRETIME mykey 返回 1767225600,过期时间被重置。
2. SET 命令会清除过期时间
使用 SET 命令覆盖 Key 的值时(不带 EXAT/PXAT/EX/PX 选项),原有的过期时间会被清除,Key 变为永久有效:
SET mykey "old" EXAT 1735689600 SET mykey "new" TTL mykey
TTL mykey 返回 -1。
3. 其他会影响过期时间的命令
| 命令 | 对过期时间的影响 |
|---|---|
| SET、GETSET | 清除过期时间(Key 变为永久有效) |
| SET ... EXAT / PXAT | 设置新的绝对过期时间 |
| SET ... EX / PX | 设置新的相对过期时间 |
| INCR / DECR / INCRBY | 保留原有过期时间 |
| LPUSH / RPUSH | 保留原有过期时间 |
| HSET / HMSET | 保留原有过期时间 |
| SADD / ZADD | 保留原有过期时间 |
| RENAME | 源 Key 的过期时间转移到目标 Key |
| COPY | 默认不复制过期时间 |
| PERSIST | 移除过期时间 |
| GETEX | 可同时修改过期属性 |
七、Redis 过期机制原理
1. 惰性过期(Passive Expiration)
当客户端访问某个 Key 时,Redis 会检查该 Key 是否已过期,如果已过期则立即删除并返回不存在。
特点:
- 对 CPU 友好,只在访问时检查
- 对内存不友好,已过期但未被访问的 Key 会一直占用内存
2. 定期过期(Active Expiration)
Redis 会周期性地(默认每秒 10 次)随机抽取一部分设置了过期时间的 Key 进行检查,删除已过期的 Key。
特点:
- 限制每次检查的数量和耗时,避免阻塞
- 通过随机采样逐步清理过期 Key
3. 过期 Key 的复制与持久化
- 主从复制:主节点删除过期 Key 后,会向所有从节点发送 DEL 命令。从节点不会主动过期 Key。
- AOF 持久化:Key 过期被删除时,会向 AOF 文件追加一条 DEL 命令。
- RDB 持久化:过期 Key 不会被写入 RDB 文件。
八、EXPIREAT 与不同数据类型
EXPIREAT 作用于整个 Key,所有数据类型(String、Hash、List、Set、Sorted Set、Stream 等)的过期行为一致。
HSET user:1001 name "Alice" age 30 EXPIREAT user:1001 1735689600
RPUSH queue:jobs job-a job-b job-c EXPIREAT queue:jobs 1735689600
SADD tags redis python java EXPIREAT tags 1735689600
ZADD leaderboard 100 "player1" 200 "player2" EXPIREAT leaderboard 1735689600
过期后整个 Key 及其所有数据都会被删除。
九、事务、Pipeline 与并发场景
1. 在事务中使用
EXPIREAT 可以放入事务队列:
MULTI SET promo:banner "summer-sale" EXPIREAT promo:banner 1735689600 EXEC
EXEC 执行后返回各命令的结果。
2. 原子性与 SET … EXAT
“先 SET 再 EXPIREAT” 不是原子操作。如果 SET 成功后客户端崩溃或网络中断,EXPIREAT 可能不会执行,导致 Key 永久有效。
推荐使用原子的 SET ... EXAT 形式(Redis 6.2+):
SET promo:banner "summer-sale" EXAT 1735689600
3. 分布式锁续期
使用 EXPIREAT 续期分布式锁时,应使用 XX 选项确保锁仍然存在:
EXPIREAT lock:resource 1735689600 XX
如果返回 0,说明锁已经不存在(可能已过期或被释放)。
4. Pipeline 批量设置
需要为大量 Key 设置同一过期时间点时,可以使用 Pipeline 批量发送 EXPIREAT 命令,减少网络往返。
十、在常见客户端中的使用方式
1. redis-cli
redis-cli SET promo:key "summer-sale" redis-cli EXPIREAT promo:key 1735689600 redis-cli EXPIRETIME promo.key
结合 Shell 动态计算时间戳:
# 设置明天同一时间过期 redis-cli EXPIREAT mykey $(( $(date +%s) + 86400 )) # 设置 2025-06-01 00:00:00 UTC 过期 redis-cli EXPIREAT mykey $(date -d "2025-06-01 00:00:00 UTC" +%s)
2. Python(redis-py)
import redis
import time
client = redis.Redis(host="localhost", port=6379, decode_responses=True)
client.set("promo:key", "summer-sale")
# 设置在指定 Unix 时间戳过期
expire_at = int(time.time()) + 3600 # 1 小时后
client.expireat("promo:key", expire_at)
# 使用 datetime 对象(redis-py 支持)
from datetime import datetime, timezone, timedelta
expire_dt = datetime(2025, 6, 1, 0, 0, 0, tzinfo=timezone.utc)
client.expireat("promo:key", expire_dt)
# 查看过期时间戳
expire_time = client.expiretime("promo:key")
print(expire_time)
3. Node.js(node-redis)
import { createClient } from "redis";
const client = createClient();
await client.connect();
await client.set("promo:key", "summer-sale");
// 设置在指定 Unix 时间戳过期
const expireAt = Math.floor(Date.now() / 1000) + 3600; // 1 小时后
await client.expireAt("promo:key", expireAt);
// 查看过期时间戳
const expireTime = await client.expireTime("promo:key");
console.log(expireTime);
await client.quit();4. Java(Jedis)
import java.time.Instant;
import redis.clients.jedis.Jedis;
try (Jedis jedis = new Jedis("localhost", 6379)) {
jedis.set("promo:key", "summer-sale");
// 设置在指定 Unix 时间戳过期
long expireAt = Instant.now().getEpochSecond() + 3600; // 1 小时后
long result = jedis.expireAt("promo:key", expireAt);
System.out.println(result); // 1
// 使用 Date 对象
import java.util.Date;
jedis.expireAt("promo:key", Date.from(Instant.parse("2025-06-01T00:00:00Z")));
// 查看过期时间戳
long expireTime = jedis.expireTime("promo:key");
System.out.println(expireTime);
}
十一、典型使用场景
1. 限时促销活动
促销活动在指定日期结束,自动清理相关 Key:
SET promo:summer:2025 "active" EXPIREAT promo:summer:2025 1751328000 # 2025-07-01 00:00:00 UTC
活动到期后 Key 自动失效,无需手动清理。
2. 定时任务触发
利用 Key 过期事件(keyspace notification)在指定时间触发任务:
SET task:send-email:1001 "data" EXAT 1735689600
配合 notify-keyspace-events Ex 配置,订阅过期事件来触发定时任务。
3. 限时验证码
验证码在特定时间点失效:
# 设置验证码在当天 23:59:59 过期 redis-cli SET sms:code:13800138000 "123456" redis-cli EXPIREAT sms:code:13800138000 $(date -d "today 23:59:59" +%s)
4. 会话固定时间失效
用户会话在当天结束时间点自动失效:
redis-cli SET session:abc123 "user:1001" redis-cli EXPIREAT session:abc123 $(date -d "tomorrow 00:00:00" +%s)
5. 倒计时活动
秒杀活动在特定时刻开始或结束:
SET seckill:stock "100" EXPIREAT seckill:stock 1735689600 # 活动结束时间
6. 数据归档
日志或统计数据在指定日期后自动清理:
SET log:2025-01:access "count_data" EXPIREAT log:2025-01:access 1767225600 # 2026-01-01 自动清理 2025 年 1 月数据
十二、性能与使用建议
- 时间复杂度:O(1)。
- EXPIREAT 是写命令(ACL 类别为 @keyspace、@write、@fast),会修改 Key 的元数据。
- 优先使用 SET key value EXAT timestamp 的原子形式,避免 SET 成功但 EXPIREAT 失败导致 Key 永久有效。
- 注意时区问题:EXPIREAT 接受的是 UTC Unix 时间戳,不是本地时间。使用 date 命令时注意时区转换。
- 大量 Key 在同一时间戳过期可能导致 Redis 瞬间压力增大(缓存雪崩)。可以在过期时间上加入随机偏移,打散过期时间。
- 时间戳在过去时 Key 会立即过期,等效于删除。如需删除 Key,优先使用 DEL 或 UNLINK。
- Redis 7.0+ 的条件选项(NX、XX、GT、LT)可以在很多场景下替代"先 TTL 判断再设置"的非原子流程,使用更安全。
- 集群模式下,EXPIREAT 作用于单个 Key,不受哈希槽限制。
- 服务器时间偏差会影响过期判断。确保 Redis 服务器系统时间准确,避免时间跳转导致 Key 提前或延后过期。
十三、常见问题排查
问题 1:EXPIREAT 返回 1 但 Key 立即过期
可能的原因:
- 传入的时间戳小于当前时间戳(在过去),Key 会立即过期。
- 系统时间不准确,导致时间戳判断异常。
- 传入的是毫秒时间戳(而非秒级时间戳),导致值远大于预期,但如果误传小毫秒值则可能已过期。
TIME EXPIRETIME mykey TTL mykey
问题 2:EXPIREAT 返回 0
可能的原因:
- Key 不存在(先 EXISTS 确认)。
- 使用了条件选项(NX/XX/GT/LT)但条件不满足。
- 时间戳格式不正确(必须是整数)。
问题 3:Key 没有在预期时间过期
可能的原因:
- 时区计算错误:EXPIREAT 使用 UTC 时间戳,而用户可能误用了本地时间。
- 系统时间被修改或存在偏差。
- 在 EXPIREAT 之后又执行了 SET 等会清除过期时间的命令。
- Key 被 PERSIST 移除了过期时间。
- 内存达到 maxmemory 上限,触发了内存淘汰策略,Key 被提前淘汰。
问题 4:误用毫秒时间戳
EXPIREAT 接受秒级时间戳,PEXPIREAT 接受毫秒级时间戳。如果误将毫秒时间戳传给 EXPIREAT,会导致过期时间远超预期(大约 1000 倍)。
# 错误:传入了毫秒时间戳 EXPIREAT key 1735689600000 # 实际过期时间约为 56670 年后 # 正确:秒级时间戳 EXPIREAT key 1735689600 # 如果需要毫秒精度,使用 PEXPIREAT PEXPIREAT key 1735689600000
问题 5:从节点数据不一致
从节点不会主动过期 Key,而是等待主节点的 DEL 命令。如果从节点读取到"应该已过期"的 Key,这是正常行为。确保读取策略与业务需求匹配。
十四、完整练习
下面的示例演示从设置绝对过期时间、验证 TTL 和 EXPIRETIME、条件选项、更新过期时间,到与 SET ... EXAT、PERSIST 等命令配合使用的完整流程:
DEL demo:key DEL demo:perm DEL demo:renew # 获取当前时间 TIME # 基本用法:设置绝对过期时间 SET demo:key "hello" EXPIREAT demo:key 1767225600 EXPIRETIME demo:key TTL demo:key # 对不存在的 Key 设置过期 EXPIREAT demo:noexist 1767225600 # SET 会清除过期时间 SET demo:key "new value" TTL demo:key # 使用 SET ... EXAT 原子设置 SET demo:key "with ttl" EXAT 1767225600 EXPIRETIME demo:key # PERSIST 移除过期时间 PERSIST demo:key TTL demo:key # 条件选项示例(需要 Redis 7.0+) # NX:仅当无过期时间时设置 SET demo:perm "permanent" EXPIREAT demo:perm 1767225600 NX EXPIRETIME demo:perm EXPIREAT demo:perm 1735689600 NX # 返回 0,因为已有过期时间 # XX:仅当有过期时间时更新 EXPIREAT demo:perm 1735689600 XX EXPIRETIME demo:perm # 续期场景模拟 SET demo:renew "session" EXAT 1735689600 EXPIRETIME demo:renew EXPIREAT demo:renew 1767225600 XX # 续期到更晚的时间 EXPIRETIME demo:renew # Hash 类型的过期 HSET demo:hash name "Alice" age 30 EXPIREAT demo:hash 1767225600 EXPIRETIME demo:hash # 验证过去时间戳立即过期 SET demo:past "gone" EXPIREAT demo:past 1 EXISTS demo:past
预期结果:
- EXPIREAT demo:key 1767225600 返回 (integer) 1,EXPIRETIME demo:key 返回 1767225600,TTL 返回正数。
- EXPIREAT demo:noexist 1767225600 返回 (integer) 0。
- SET demo:key "new value" 后 TTL demo:key 返回 -1。
- SET demo:key "with ttl" EXAT 1767225600 后 EXPIRETIME demo:key 返回 1767225600。
- PERSIST demo:key 返回 1,TTL demo:key 返回 -1。
- EXPIREAT demo:perm 1767225600 NX 返回 1,第二次 NX 返回 0。
- EXPIREAT demo:perm 1735689600 XX 返回 1,EXPIRETIME 返回 1735689600。
- EXPIREAT demo:renew 1767225600 XX 返回 1,EXPIRETIME 返回 1767225600。
- EXPIREAT demo:hash 1767225600 返回 1,EXPIRETIME demo:hash 返回 1767225600。
- EXPIREAT demo:past 1 后 EXISTS demo:past 返回 (integer) 0。
十五、命令速查表
| 需求 | 命令 | 时间类型 | 精度 |
|---|---|---|---|
| 在指定 Unix 时间戳过期(秒) | EXPIREAT key timestamp | 绝对 | 秒 |
| 在指定 Unix 时间戳过期(毫秒) | PEXPIREAT key timestamp | 绝对 | 毫秒 |
| 多少秒后过期 | EXPIRE key seconds | 相对 | 秒 |
| 多少毫秒后过期 | PEXPIRE key ms | 相对 | 毫秒 |
| 原子设置值和绝对过期时间 | SET key value EXAT timestamp | 绝对 | 秒 |
| 原子设置值和绝对过期时间 | SET key value PXAT timestamp | 绝对 | 毫秒 |
| 获取剩余生存时间(秒) | TTL key | — | 秒 |
| 获取剩余生存时间(毫秒) | PTTL key | — | 毫秒 |
| 获取过期时间戳(秒) | EXPIRETIME key | — | 秒 |
| 获取过期时间戳(毫秒) | PEXPIRETIME key | — | 毫秒 |
| 移除过期时间 | PERSIST key | — | — |
| 仅当无过期时间时设置 | EXPIREAT key timestamp NX | 绝对 | 秒 |
| 仅当有过期时间时更新 | EXPIREAT key timestamp XX | 绝对 | 秒 |
| 仅当新过期时间更晚时设置 | EXPIREAT key timestamp GT | 绝对 | 秒 |
| 仅当新过期时间更早时设置 | EXPIREAT key timestamp LT | 绝对 | 秒 |
总结
EXPIREAT 的核心作用是为 Key 设置以秒级 Unix 时间戳为单位的绝对过期时间,到期后 Redis 自动删除该 Key:
EXPIREAT key timestamp [NX | XX | GT | LT]
使用时重点注意五点:
- EXPIREAT 使用 UTC 秒级 Unix 时间戳,注意时区转换和精度(秒而非毫秒)。
- 时间戳在过去时 Key 会立即过期,等效于删除。
- 优先使用 SET key value EXAT timestamp 的原子形式,避免 SET 成功但 EXPIREAT 失败。
- SET 等覆盖 Key 的命令会清除原有过期时间;INCR、LPUSH、HSET 等修改内部数据的命令则保留过期时间。
- Redis 7.0+ 支持 NX、XX、GT、LT 条件选项,可以原子地实现"仅当无过期时设置"、"续期"等场景。
到此这篇关于Redis中EXPIREAT命令实现的文章就介绍到这了,更多相关Redis EXPIREAT命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
redis中使用redis-dump导出、导入、还原数据实例
这篇文章主要介绍了redis中使用redis-dump导出、导入、还原数据实例,本文直接给出操作命令,并给出注释加以说明,需要的朋友可以参考下2014-11-11
redis Template.opsForValue()中方法实例详解
这篇文章主要介绍了redis Template.opsForValue()中方法讲解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-05-05


最新评论