本文内容为 http://redis.io/commands/setex 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
SETEX key seconds value

将键 key 对应的值设置为字符串值 value,且将键 key 设置为在给定(Seconds)后过期。

Set key to hold the string value and set key to timeout after a given number of seconds.

该命令等同于执行以下命令:

SET mykey value
EXPIRE mykey seconds

区别在于,SETEX 是原子的(atomic)。当然,将上述两条命令置于 MULTI / EXEC 中也能实现同样的效果。SETEX 相比命令序列可以作为更加快速方便的选择,因为当 Redis 被用作缓存时,该操作非常普遍。

SETEX is atomic, and can be reproduced by using the previous two commands inside an MULTI / EXEC block. It is provided as a faster alternative to the given sequence of operations, because this operation is very common when Redis is used as a cache.

当秒数 seconds 无效时,将会返回一个错误。

An error is returned when seconds is invalid.