HSETEX
设置给定哈希键的一个或多个字段的值,并可选择设置其过期时间。
语法
HSETEX key [FNX | FXX] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL] FIELDS numfields field value [field value ...]
引入版本:8.0.0
复杂度摘要:O(N)
设置给定哈希键的一个或多个字段的值,并可选择设置其过期时间或生存时间 (TTL)。如果给定的键已经拥有一个值,则该值将被覆盖,并且与该键关联的任何先前的 TTL 都将被丢弃。
必需参数
key
保存哈希值的键的名称。
FIELDS numfields field value [field value ...]
要设置的哈希字段值对。 numfields 是字段的数量,后面跟着许多字段值对。
可选参数
以下选项修改命令的行为。 EX、PX、EXAT、PXAT 和 KEEPTTL 选项是互斥的。
FNX
仅当这些字段都不存在时才设置这些字段。 FNX 和 FXX 是互斥的。
FXX
仅当所有字段均已存在时才设置这些字段。
EX seconds
设置指定的过期时间,以秒为单位。
PX milliseconds
设置指定的过期时间,单位为毫秒。
EXAT unix-time-seconds
设置字段到期的指定 Unix 时间(以秒为单位)。
PXAT unix-time-milliseconds
设置字段到期的指定 Unix 时间(以毫秒为单位)。
KEEPTTL
保留与字段关联的 TTL。
示例
redis> HSETEX mykey EXAT 1740470400 FIELDS 2 field1 "Hello" field2 "World"
(integer) 1
redis> HTTL mykey FIELDS 2 field1 field2
1) (integer) 55627
2) (integer) 55627
redis> HSETEX mykey FNX EX 60 FIELDS 2 field1 "Hello" field2 "World"
(integer) 0
redis> HSETEX mykey FXX EX 60 KEEPTTL FIELDS 2 field1 "hello" field2 "world"
(error) ERR Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments can be specified
redis> HSETEX mykey FXX KEEPTTL FIELDS 2 field1 "hello" field2 "world"
(integer) 1
redis> HTTL mykey FIELDS 2 field1 field2
1) (integer) 55481
2) (integer) 55481
Redis Software 与 Redis Cloud 兼容性
| Redis 软件 |
Redis Cloud |
说明 |
|---|---|---|
| ✅ 标准 |
✅ 标准 |
返回值
RESP2
以下之一: * 整数回复:如果未设置字段,则为 0。 * 整数回复: 1(如果所有字段均已设置)。
RESP3
资料来源:Redis 官方 HSETEX 文档;中文内容参考 Redis 文档许可。